angular.js - angularjs $http.get cannot cross domain problem
phpcn_u1582
phpcn_u1582 2017-05-15 16:51:47
0
3
676

A very simple $http.get method to request data from the interface. But I just don’t know why, and it keeps saying that it cannot cross domain. How to solve it? (PS: After adding API_HOST_URL to another page, the post value goes to that path, and no cross-domain errors are reported, but the same copy does not work under the get method)

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('请求失败了');
 })
}

Error content:
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.

Changed to write using jsonp:

 $http.jsonp(API_HOST_URL+"/ss/test.do?jsonp=JSON_CALLBACK&pNo="+array)

Error: Uncaught SyntaxError: Unexpected token:

phpcn_u1582
phpcn_u1582

reply all(3)
给我你的怀抱

1) Cross-domain has nothing to do with what kind of framework is used,
(PS: After adding API_HOST_URL to another page, the post value goes to that path, and no cross-domain error is reported, but the same is copied and used in get The method does not work) Is the other page the same domain as your page? Is it the same problem when using get/jsonp on another page?
2) After using JSONP, please check whether the returned data content meets the json format requirements. If not, a similar error will be reported
Uncaught SyntaxError: Unexpected token :

小葫芦

You may need to add some headers:

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();
});
Ty80

If you want to cross-domain, jsonp is preferred, and then ensure that the server returns it in the normal js syntax format
For example, the return value is:
var test = {"status":1,"data":5};

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template