即使接收到200,angularjs $ http.jsonp也会转到.error而不是.success [英] angularjs $http.jsonp goes to .error instead of .success even when 200 is received
问题描述
我正在构建一个AngularJS应用程序,该应用程序调用从数据库获取数据的NodeJS服务器.NodeJS返回JSON.stringify(someArrayWithData).
I'm building an AngularJS app which calls a NodeJS server that gets data from a DB. The NodeJS returns a JSON.stringify(someArrayWithData).
这是AngularJS代码:
Here is the AngularJS code:
$scope.getMainGroups = function(){
$http.jsonp("http://127.0.0.1:3000/get/MainGroups?callback=JSON_CALLBACK").success(function(data, status, headers, config) {
$scope.MainGroups = data;
}).error(function(data, status, headers, config) {
$scope.MainGroups = status;
});
};
$ scope.MainGroups将转到.error而不是成功,即使我在chrome调试器中看到返回的结果(200 ok)也是如此.
$scope.MainGroups is going to the .error instead to the success even when I can see in the chrome debugger that the result came back (200 ok).
我想念什么?
推荐答案
您必须在服务器端返回有效的JSON响应.200 OK只是注入到DOM中的GET请求.我敢打赌,您不会从服务器返回带有正确响应代码的有效JSON响应.
You must return a valid JSON response on the server side. The 200 OK is just the GET request that is injected into the DOM. I bet you are not returning a valid JSON response from the server with right response code.
以下是有关如何在服务器端(PHP)上构造响应的示例:简单的jQuery,PHP和JSONP示例?
Here is an example on how to construct the response on the server side (PHP): Simple jQuery, PHP and JSONP example?
这篇关于即使接收到200,angularjs $ http.jsonp也会转到.error而不是.success的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!