请求的资源上没有 'Access-Control-Allow-Origin' header,即使它存在
No 'Access-Control-Allow-Origin' header is present on the requested resource even though it is present
我正在使用 Django + Phonegap 构建一个应用程序。当我尝试使用此函数发送 Ajax 请求时:
<script>
$.ajax({
url: "http://192.168.0.101/commerce/product/" + localStorage.getItem("toView"),
type: "GET",
data: {},
success: function (json) {
console.log(json);
}
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
我在 Chrome 控制台中收到一条错误消息:
XMLHttpRequest cannot load http://192.168.0.101/commerce/product/2.
Redirect from 'http://192.168.0.101/commerce/product/2' to
'http://192.168.0.101/commerce/product/2/' has been blocked by CORS
policy: No 'Access-Control-Allow-Origin' header is present on the
requested resource. Origin 'http://192.168.0.101:3000' is therefore
not allowed access.
问题在于我包含了所请求的 header。当我在 chrome 中打开给定的 URL 并查看服务器响应时,我得到了这个。
HTTP/1.0 200 OK
Date: Tue, 16 May 2017 09:42:29 GMT
Server: WSGIServer/0.2 CPython/3.4.3
Content-Type: application/json
Access-Control-Allow-Origin: *
X-Frame-Options: SAMEORIGIN
Access-Control-Allow-Methods: OPTIONS,GET,PUT,POST,DELETE
Access-Control-Allow-Headers: X-Requested-With, Content-Type
Content-Length: 89
我也在 views.py
中包含了我的服务器代码。
def product(request, prod_id):
#### SOME CODE
response = JsonResponse(response_data)
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'OPTIONS,GET,PUT,POST,DELETE'
response['Access-Control-Allow-Headers'] = 'X-Requested-With, Content-Type'
return response
为什么会出现此错误?请帮忙。谢谢。
您应该在 ajax
请求中的 url 末尾添加一个斜杠:
url: "http://192.168.0.101/commerce/product/" + localStorage.getItem("toView") + '/',
Django 期望在 URL.
后面附加一个斜杠
我正在使用 Django + Phonegap 构建一个应用程序。当我尝试使用此函数发送 Ajax 请求时:
<script>
$.ajax({
url: "http://192.168.0.101/commerce/product/" + localStorage.getItem("toView"),
type: "GET",
data: {},
success: function (json) {
console.log(json);
}
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
我在 Chrome 控制台中收到一条错误消息:
XMLHttpRequest cannot load http://192.168.0.101/commerce/product/2. Redirect from 'http://192.168.0.101/commerce/product/2' to 'http://192.168.0.101/commerce/product/2/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.101:3000' is therefore not allowed access.
问题在于我包含了所请求的 header。当我在 chrome 中打开给定的 URL 并查看服务器响应时,我得到了这个。
HTTP/1.0 200 OK
Date: Tue, 16 May 2017 09:42:29 GMT
Server: WSGIServer/0.2 CPython/3.4.3
Content-Type: application/json
Access-Control-Allow-Origin: *
X-Frame-Options: SAMEORIGIN
Access-Control-Allow-Methods: OPTIONS,GET,PUT,POST,DELETE
Access-Control-Allow-Headers: X-Requested-With, Content-Type
Content-Length: 89
我也在 views.py
中包含了我的服务器代码。
def product(request, prod_id):
#### SOME CODE
response = JsonResponse(response_data)
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'OPTIONS,GET,PUT,POST,DELETE'
response['Access-Control-Allow-Headers'] = 'X-Requested-With, Content-Type'
return response
为什么会出现此错误?请帮忙。谢谢。
您应该在 ajax
请求中的 url 末尾添加一个斜杠:
url: "http://192.168.0.101/commerce/product/" + localStorage.getItem("toView") + '/',
Django 期望在 URL.
后面附加一个斜杠