重新排列 | CustomPOST :我无法发出 CORS POST 请求,正文中有 json 数据对象
Restangular | CustomPOST : I am unable to make a CORS POST request, with json data object in the body
我正在尝试通过以下方式使用 Restangular 发出 CORS POST 请求:
//data is a json object
var urlToPost = '/api/deal/update/'+dealId;
return Restangular.all(urlToPost).customPOST(data, '', {}, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, PUT, DELETE, OPTIONS"
});
当我拨打电话时,控制台上出现以下错误:
XMLHttpRequest cannot load http://55.76.122.145:8080/hulk/api/deal/update/55cd93bd20ce9744aeebff3a. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9001' is therefore not allowed access.
并且在“网络”选项卡中,我可以看到使用 OPTION
方法触发 successful
调用 status 200
。
怎么办?我做错了什么?
错误消息为您说明了这一点:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9001' is therefore not allowed access.
服务器必须响应 OPTION请求响应包括Access-Control-Allow-Origin
header 告诉浏览器允许访问提供 JavaScript 的网站。
Access-Control-Allow-Origin
和 Access-Control-Allow-Methods
不是 请求 header。您不能编写 JavaScript 来授予自己从其他人的网站读取数据的权限。那将是愚蠢的。
我正在尝试通过以下方式使用 Restangular 发出 CORS POST 请求:
//data is a json object
var urlToPost = '/api/deal/update/'+dealId;
return Restangular.all(urlToPost).customPOST(data, '', {}, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, PUT, DELETE, OPTIONS"
});
当我拨打电话时,控制台上出现以下错误:
XMLHttpRequest cannot load http://55.76.122.145:8080/hulk/api/deal/update/55cd93bd20ce9744aeebff3a. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9001' is therefore not allowed access.
并且在“网络”选项卡中,我可以看到使用 OPTION
方法触发 successful
调用 status 200
。
怎么办?我做错了什么?
错误消息为您说明了这一点:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9001' is therefore not allowed access.
服务器必须响应 OPTION请求响应包括Access-Control-Allow-Origin
header 告诉浏览器允许访问提供 JavaScript 的网站。
Access-Control-Allow-Origin
和 Access-Control-Allow-Methods
不是 请求 header。您不能编写 JavaScript 来授予自己从其他人的网站读取数据的权限。那将是愚蠢的。