我试图让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;
}
});
Run Code Online (Sandbox Code Playgroud)
当我使用chrome的devtools查看网络时,我发现'Access-Control-Allow-Origin'确实没有标头.但是当我手动加载网站时它就存在了!
我使用以下代码来设置标头:
response = JsonResponse(simpleWeek)
response['Access-Control-Allow-Origin'] = '*'
return response
Run Code Online (Sandbox Code Playgroud)
希望得到一些帮助!