[英]Angular http post request - No 'Access-Control-Allow-Origin' header is present on the requested resource
I am trying to send data to servlet using angular http post, 我正在尝试使用angular http post将数据发送到servlet,
var httpPostData = function (postparameters,postData){
var headers = {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS',
'Accept': 'application/json'
};
return $http ({
method : 'POST',
url : 'http://localhost:8080/json/jasoncontroller',
params : postparameters,
headers: headers,
data : postData
}).success (function (responseData){
return responseData.data;
})
}
But i am keep on getting error that No 'Access-Control-Allow-Origin' header is present on the requested resource. 但我继续得到错误, 请求资源上没有'Access-Control-Allow-Origin'标头。 Origin 'null' is therefore not allowed access. 因此不允许原点'null'访问。
I did have set following headers on my servlet 我确实在我的servlet上设置了以下标题
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.addHeader("Access-Control-Max-Age", "3600");
response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if i remove data from http post it works fine, but no luck with data. 如果我从http帖子中删除数据它工作正常,但数据没有运气。
Actually what happens is in some frameworks two calls are made, 实际上在一些框架中发生了两次调用,
OPTIONS require just empty answer 200 OK 选项只需要空答案200 OK
if(request.methord == 'OPTIONS'){
res.send(200);
} else {
next();
}
You can also install this chrome extension to enable cors, that is the easy way out ! 你也可以安装这个chrome扩展来启用cors,这是最简单的方法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.