I am trying to create a JSONP request using the AngularJS $ http service, and on errors I get the wrong status code 404 instead of 500, and the body of the page is also missing.
So here is the scenario:
The url that I call returns 500 Internal Server Error with JSON output containing an error message:
http://private.peterbagi.de/jsfiddle/ng500status/api.php?code=500&callback=test
index.html
(see it in action: http://private.peterbagi.de/jsfiddle/ng500status/ )
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> <script src= "app.js"></script> </head> <body ng-app="app" ng-controller="UploadController"> <button ng-click="upload(200)" value="OK">OK</button> <button ng-click="upload(500)" value="Fail">Fail</button> <pre>{{response | json}}</pre> </body> </html>
app.js
var app = angular.module('app', []); app.constant('BASE_URL','http://private.peterbagi.de/jsfiddle/ng500status/'); app.controller("UploadController", ['$scope','Upload','BASE_URL', function($scope,Upload,BASE_URL) { $scope.upload = function(code) { Upload(code).then( function(response) { $scope.response = response; }, function(error) { $scope.response = error; } ); } }]); app.factory('Upload', ['$http','BASE_URL', function($http,BASE_URL) { return function (code) { var callUrl = BASE_URL + "api.php?code="+code; return $http({ method: 'JSONP', url : callUrl+"&callback=JSON_CALLBACK" }); } }]);
When you click the Fail button, 404 status is returned, and the response body is also missing.
Exit
{ "status": 404, "config": { "method": "JSONP", "transformRequest": [ null ], "transformResponse": [ null ], "url": "http://private.peterbagi.de/jsfiddle/ng500status/api.php?code=500&callback=JSON_CALLBACK", "headers": { "Accept": "application/json, text/plain, */*" } }, "statusText": "error" }
In the Chrome panel of DevTools Network you will see the correct response code (500), and I expect to see the same result in the output of Angular.
Why is this happening or what am I doing wrong?
Update:
I built a similar example, but with GET requests instead of JSONP, and it works well: http://private.peterbagi.de/jsfiddle/ng500status2/
This seems to be a specific JSONP issue. However, this does not solve my original problem, because I have to work with cross-domain queries. Any ideas?