简体   繁体   中英

ajax request works with $ but not angular

I have some jQuery that fetches data and executes a callback:

$(function(){
$.ajax({
    type: 'POST',
    url: 'http://flash-cast-hrd.appspot.com/api/sonar/sync', 
    data: {
        source: 5,
        auth: 'JXEI....DHA2J'
    }, 
    success: displayData
});
});

I am trying to make the same request from my angular controller, where I have a method on the scope.

$scope.init = function(){

            $http.post('http://flash-cast-hrd.appspot.com/api/sonar/sync', {
                source: 5,
                auth: 'JXEI....DHA2J'
            })
                .success(function(data){
                    console.log(data);
                })
                .error(function(error){
                    console.log('Error is: ' + error);
                });
        };

Then call it in a view:

<div ng-init="init()"></div>

When I load the page I get an error in the console:

OPTIONS http://flash-cast-hrd.appspot.com/api/sonar/sync 405 (Method Not Allowed) angular.js:8021
OPTIONS http://flash-cast-hrd.appspot.com/api/sonar/sync No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. angular.js:8021
XMLHttpRequest cannot load http://flash-cast-hrd.appspot.com/api/sonar/sync. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. :8000/routes:1
Error is:  

How can I make the angular $http.post request work?

问题是由于跨域访问引起的,您可以通过在服务器上设置proxypass来克服,并且您的请求还需要包含Access-Control-Allow-header。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM