【发布时间】:2016-06-02 00:11:16
【问题描述】:
我试图让 CORS 请求正常工作。使用以下 JS 代码,我收到此错误:XMLHttpRequest cannot load http://localhost:65491/?token=u80h9kil9kjuu02539buak4r6n&user=~me. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:50303' is therefore not allowed access.
这是JS代码:
$.ajax({
url: "http://localhost:60906/",
data: {token : 'u80h9kil9kjuu02539buak4r6n', user : '~me'},
type: "GET",
crossDomain: true,
success: function( response ) {
alert('Success!' + response);
var context = response;
}
});
当我使用 chrome 的 devtools 查看网络时,我发现确实没有 'Access-Control-Allow-Origin' 标头。但是当我手动加载网站时,它就存在了!
我使用以下代码设置标题:
response = JsonResponse(simpleWeek)
response['Access-Control-Allow-Origin'] = '*'
return response
希望得到帮助!
【问题讨论】:
-
能否请您显示更多您的观点的代码?
-
您是否尝试将 crossDomain 设置为 true?默认为假。
-
@gaemaf 这行得通,但现在我得到了:
Failed to load resource: net::ERR_CONNECTION_REFUSED我使用了一个干净的 Chrome 版本(就像我在测试代码时经常做的那样),所以没有导致问题的广告拦截器。 没关系,网址中的拼写错误,它仍然不起作用!我现在确实遇到了另一个错误。我现在正在编辑问题。
标签: jquery ajax django cors django-cors-headers