gpt4 book ai didi

javascript - 对同一域发出 CORS 错误的获取请求

转载 作者:行者123 更新时间:2023-11-30 00:05:53 24 4
gpt4 key购买 nike

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

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

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

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

只有在我们向其他域发出调用时,CORS 才会出现。但在这里我正在调用同一个域。您可以在 url xhr.open('GET', window.location.href, true);

中看到这一点

我在这里缺少什么?

enter image description here

最佳答案

您可以使用网络服务,例如 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/

我能够让它在这里工作:

Preview

关于javascript - 对同一域发出 CORS 错误的获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38580731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com