angularjs $http.get无法跨域问题
很简单的段$http.get向接口请求数据的方法。但是就是不知道什么原因,一直提示无法跨域。请问该怎么解决?(PS:在另外一个页面加入API_HOST_URL后,post值到那个路径,没有报过任何无法跨域的报错,但是同样的复制过来,在get方法下却不管用)
function togetpNo($scope,$http){
var array=$scope.array;
$http.get(API_HOST_URL+'/ss/test.do?pNo='+array)
.success(function(data){
alert(data);
}).error(function(e){
alert('请求失败了');
})
}
报错内容:
XMLHttpRequest cannot load
http://datatest.dev:8081/ss/test.do?pNo=h01,h02
. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '
http://localhost:8080
' is therefore not allowed access.
改成用jsonp写:
$http.jsonp(API_HOST_URL+"/ss/test.do?jsonp=JSON_CALLBACK&pNo="+array)
报错:Uncaught SyntaxError: Unexpected token :
月夜下的尛尛
10 years ago
Answers
你可能需要加上一些header:
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
航海♂日志
answered 10 years ago