angular.js - $http callback function and return code issues
过去多啦不再A梦
过去多啦不再A梦 2017-05-24 11:37:44
0
1
876
有一个rest服务,我的代码大致是这样

$http.jsonp(url)

.then(function(data){
    console.log(data)
},function(res){
    console.log(res)
})

When there is no data in the database, the console network information displays status as 200

response return data

Information printed by console.log(res)

Why does the status displayed in the network be 200, but when printed it becomes 404, and the response data is not returned. Now I need to get returncode=-1 for judgment when there is no data in the database. How should I get this response?

过去多啦不再A梦
过去多啦不再A梦

reply all(1)
滿天的星座

The URL address needs to add ?callback=JSON_CALLBACK. The result of your Response is: ({"returnCode": "-1"})。正常Response的结果是:/**/angular.callbacks._0({"found":12,"posts": []});
The following is an online example: angularjs-http-jsonp-service-example. Remove the callback query parameter and take a look at the result.


Why the status displayed by the developer tools is 200? It just means that the HTTP request is successful. Why is 404 returned?

if (event) {
  // JS成功加载但回调未被调用
  if (event.type === 'load' && 
    !callbacks.wasCalled(callbackPath)) {
          event = { type: 'error' };
        }
        text = event.type;
        status = event.type === 'error' ? 404 : 200;
  }
}

Please refer to the AngularJS httpBackend source code for details.

jsonpReq only ever sets status to 200 (OK), 404 (ERROR) or -1 (WAITING) - httpBackend.js#65

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template