对同一域发出 get 请求,出现 CORS 错误

2024-05-08

在浏览器扩展中,这是我的 ajax 调用

var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href, true);
xhr.responseType = "arraybuffer";
xhr.onload = function(event) {
  alert(this.response);
};

我真的不明白为什么这会给我这个错误

跨源请求被阻止:同源策略不允许读取远程资源http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf。 (原因:CORS 标头“Access-Control-Allow-Origin”丢失)

只有当我们调用其他域时,CORS 才会出现。但在这里我正在对同一个域进行调用。你可以在 url 中见证这一点xhr.open('GET', window.location.href, true);

我在这里缺少什么?


您可以使用网络服务,例如CrossOrigin.me https://crossorigin.me/超越它。我使用了这样的代码,它确实对我有用:

$.ajax({
  url: 'https://crossorigin.me/http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf',
  jsonpCallback: 'callback',
  type: 'GET',
  success: function (data) {
    console.log(data);
  }
});

Fiddle: http://jsfiddle.net/L84u10yj/ http://jsfiddle.net/L84u10yj/

我能够让它在这里工作:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对同一域发出 get 请求,出现 CORS 错误 的相关文章

随机推荐