我试图使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.
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'确实没有标题。但是,当我手动加载该网站时,它就存在了!
'Access-Control-Allow- Origin'
我使用以下代码设置标题:
response = JsonResponse(simpleWeek) response['Access-Control-Allow-Origin'] = '*' return response
希望能有所帮助!
它说No 'Access-Control-Allow-Origin' header is present on the requested resource.这意味着您的服务器应用程序需要调整以接受跨源请求。由于安全原因,默认情况下跨源请求不起作用。您需要启用它们。
No 'Access-Control-Allow-Origin' header is present on the requested resource.
对于django,有一个维护良好的软件包,其中包含大量用于此目的的设置:https : //github.com/ottoyiu/django-cors- headers/