AngularJS $http error function never called

后端 未结 5 1006
陌清茗
陌清茗 2020-12-09 03:22

I have that simple code :

$http.get(\"/api/test\")
    .success(function (data, status, headers, config) {
        console.log(data);
        return data;
           


        
5条回答
  •  囚心锁ツ
    2020-12-09 03:45

    I had the same problem and, honestly, follow the hints of this post put me in the wrong direction... so, I share my case/solution so other in my same situation could save time.

    I'm using Angular.js 1.2.14 + WebApi 2. this my response for a NotFound status:

    Cache-Control:no-cache
    Content-Length:0
    Date:Sat, 15 Mar 2014 14:28:35 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/8.0
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET
    X-SourceFiles:=?UTF-8?B?QzpcU3ZpbHVwcG9EaXNjaXR1clxhcGlcTWFnMTRcYXBpXGxlc3Nvblw4NA==?=
    

    As you can see, Content-Lenght:0, but that's ok.

    My problem was the uncorrect use of Angular.js interceptor, in particular something like this:

    responseError: function (result) {
                    // check something 
                    return result;
                }
    

    returning result without throw an exception or rejecting the promises (as written in docs) makes Angular believe that I want to convert rejection in correct resolution and, after that, success callback is called.

    I correct my code as follow:

    responseError: function (result) {                    
                    // check something 
                    return $q.reject(result);
                }
    

提交回复
热议问题