取得对相同域的请求,并给出CORS错误 [英] Making get request for same domain giving CORS error

查看:148
本文介绍了取得对相同域的请求,并给出CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器扩展程序中,这是我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆