取得对相同域的请求,并给出CORS错误 [英] Making get request for same domain giving CORS error
问题描述
在浏览器扩展程序中,这是我的ajax调用
In a browser extension, this my ajax call
var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href, true);
xhr.responseType = "arraybuffer";
xhr.onload = function(event) {
alert(this.response);
};
我真的不明白为什么这会给我带来错误
I really do not understand why this giving me the error
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才会出现.但是在这里,我正在拨打同一个域的电话.您可以在xhr.open('GET', window.location.href, true);
CORS comes into picture only once we are making the calls to other domain. But here I am making the call to the same domain. You can witness that in url xhr.open('GET', window.location.href, true);
我在这里想念什么?
推荐答案
您可以使用Web服务,例如 CrossOrigin.me 领先.我使用了这样的代码,它确实对我有用:
You can use a web service such as CrossOrigin.me to get ahead of it. I used a code like this and it did work for 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);
}
});
提琴: http://jsfiddle.net/L84u10yj/
Fiddle: http://jsfiddle.net/L84u10yj/
我能够在这里工作:
这篇关于取得对相同域的请求,并给出CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!