我正在使用Cordova和AngularJS构建应用。我在heroku上运行后端api。我想向api发出POST请求并向其发送JSON数据。我在服务器上设置了以下标题:
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Access-Control-Allow-origin
Access-Control-Allow-Headers: Origin, X-Requested-With, Consent-Type, Accept Via 1.1 vegur
当我运行此请求时,出现以下错误:
XMLHttpRequest cannot load https://someservername.com/api/v1.0/someroute. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mywell.app' is therefore not allowed access.
但是,当我使用curl发出请求时,它工作正常。
My Angular代码执行以下操作:
$http({
url : "https://someservername.com/api/v1.0/someroute",
method : 'POST',
headers : {"content-type" : "application/json"},
data : {"user":"ab123", "isSet":true}
}).success(function(data, status, headers, config){
console.log(data);
}).error(function(data, status, headers, config){
console.log(status);
});