问题
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