AngularJS post request doesn't get server response correctly

狂风中的少年 提交于 2019-12-14 04:12:12

问题


I'm developing a hybrid mobile application using AppGyver Steroids and AngularJS. In this mobile application I need to use the RESTful APIs of another project which I've developed; here is an example request I'm sending from the mobile app:

var data = {
    'email': $scope.userEmail,
    'password': $scope.userPassword
}

$http.post('http://example.com/api-auth-token/?format=json', data)
    .success(function(data, status, headers, config) {
        alert(data);
        $location.path('/profile');
    })
    .error(function(data, status, headers, config) {
        // show error details
        navigator.notification.alert(
            'Authentication failed. (' + status + ')',
            null,
            'Error',
            'Okay'
        );
    });

This request reaches the server perfectly well and a valid response is generated with status code 200... at least that is the case when I check the server logs. The mobile application shows a message box saying 'Authentication failed. (404)' which contradicts with what the server logs are stating.

Edit: I have also developed a native iOS app which uses these APIs and it works without any problems.


回答1:


Turns out Access-Control-Allow-Origin header was missing from responses generated by the server and this was the main culprit.



来源:https://stackoverflow.com/questions/21729938/angularjs-post-request-doesnt-get-server-response-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!