2013-04-08 8 views
24

मुझे लगता है कि सरल कोड है:

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

यह ठीक काम करता है, लेकिन त्रुटि फ़ंक्शन कभी नहीं कहा जाता है, यहां तक ​​कि जब मैं एक 404 (नहीं मिला) सर्वर से .. उस स्थिति में यह स्थिति = 404 के साथ 'सफलता' फ़ंक्शन को कॉल करता है ...

क्या यह सही है?

धन्यवाद

फ़िडलर:

Request 

GET http://localhost:41234/api/test HTTP/1.1 
Host: localhost:41234 
Connection: keep-alive 
Accept: application/json, text/plain, */* 
X-Requested-With: XMLHttpRequest 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22 
Referer: http://localhost:41234/ 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Cookie: ASP.NET_SessionId=bd1b3rib5j4beub0xbuhb1hm; FormsAuthentication=xxxxx 

Response 

HTTP/1.1 404 Not Found 
Cache-Control: no-cache 
Pragma: no-cache 
Expires: -1 
Server: Microsoft-IIS/8.0 
X-AspNet-Version: 4.0.30319 
X-SourceFiles: =?UTF-8?B?RDpcUGVzc29hxvY2FyLkFwaVxhcGcg==?= 
X-Powered-By: ASP.NET 
Content-Length: 0 

उत्तर

5

समस्या अपने वेब सर्वर, इसे 0 करने के लिए सामग्री-लंबाई स्थापित कर रही है इसका मतलब है कि कोई मान्य मान है, जैसा कि आप HTTP/1.1 specification पर देख सकते हैं है।

इसके अलावा मैंने जेएसएफडल पर त्रुटि और सफलता के उदाहरण दिखाते हुए एक उदाहरण बनाया। here देखें।

त्रुटि उदाहरण से हेडर:

अनुरोध:

GET /error/ HTTP/1.1 
Host: fiddle.jshell.net 
Connection: keep-alive 
Accept: application/json, text/plain, */* 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31 
DNT: 1 
Referer: http://fiddle.jshell.net/danielcsgomes/cAMc6/1/show/ 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: UTF-8,*;q=0.5 
Cookie: csrftoken=4tNVNxC5v6BSq9yJCKkHlGFJBz3cClqd` 

प्रतिक्रिया:

HTTP/1.1 404 NOT FOUND 
Server: nginx/0.8.54 
Date: Fri, 12 Apr 2013 00:38:07 GMT 
Content-Type: text/html; charset=utf-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Vary: Cookie 
Content-Encoding: gzip 
+3

यह गलत है। Angularjs परवाह नहीं है सामग्री की लंबाई क्या है। – AgDude

+4

यह उत्तर सही के रूप में चिह्नित नहीं किया जाना चाहिए, यह वास्तव में गलत है जैसे AgDude उल्लेख किया। मेरे लिए सही जवाब @ विल्वर से था (नीचे देखें)! – Nabi

3

आप AngularJS संस्करण 1.1.1 या अधिक से अधिक उपयोग कर रहे हैं ...

करता जोड़कर '.json' काम ?:

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

यदि ऐसा है, तो इसे भी ठीक किया जा सकता है:

myModule.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; 
}]); 

देखें इस =>Angular JS Fails After Upgrade from 1.1.0 to 1.1.1

+0

हैलो, जोड़कर .json कि त्रुटि पैदा कर रहा है: { "संदेश": "कोई HTTP संसाधन नहीं मिला जो अनुरोध यूआरआई से मेल खाता है http: // localhost: 4021/api/test.json '।", "messageDetail": "कोई प्रकार नहीं मिला जो' उपयोगकर्ता नामक नियंत्रक से मेल खाता है। जेसन '। "} ... मैं सर्वर पर .NET वेब एपीआई का उपयोग कर रहा हूं ... – Paul

+0

मैंने – Paul

+0

प्रश्न के लिए फिडलर अनुरोध/प्रतिक्रिया जोड़ा, इस विशेष स्थिति में विस्तार सहायता क्यों बदलती है ... मैं नहीं देखता यह –

24

मैं एक ही समस्या थी और, ईमानदारी से, इस पोस्ट के संकेत का पालन करें मुझे डाल गलत दिशा में ... इसलिए, मैं अपना केस/समाधान साझा करता हूं, इसलिए मेरी स्थिति में अन्य समय बचा सकता है।

मैं Angular.js 1.2.14 + WebAPI 2. एक NotFound की स्थिति के लिए यह मेरी प्रतिक्रिया का उपयोग कर रहा:

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==?= 

आप देख सकते हैं, सामग्री-लंबाई: 0, लेकिन यह ठीक है।

मेरे समस्या इस तरह विशेष कुछ में, Angular.js इंटरसेप्टर का uncorrect उपयोग किया गया था:

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

एक अपवाद फेंक बिना परिणाम लौटने या वादे (as written in docs) कोणीय बनाता खारिज विश्वास है कि मैं परिवर्तित करना चाहते हैं सही संकल्प में अस्वीकृति और उसके बाद, सफलता कॉलबैक कहा जाता है।

मैं इस प्रकार मेरी कोड सही:

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

हम एक कारखाने के लिए पोस्ट को स्थानांतरित करने, हम वास्तव में $ http सामान परीक्षण के बारे में यहाँ परवाह नहीं है, बस क्या यह सफलता या त्रुटि पर करता है:

factoryModule.factory('GiftFactory', function ($http, Settings) { 
    var saveUrl = Settings.endpoints.saveUrl; 

    return { 
     createGift: function (data) { 
      var gift = { gift: JSON.stringify(angular.toJson(data.gift)) }; 
      return $http.post(saveUrl, gift); 
     } 
    }; 
}); 

हम इसे इस तरह कहते हैं:

$scope.submit = function() { 
     $scope.submitting = true; 
     GiftFactory.createGift($scope) 
      .success(function() { 
       $window.location = Giving.endpoints.indexUrl; 
      }).error(function() { 
       $scope.submitting = false; 
       alert('an error occurred'); 
     }); 
    }; 

हम इसे इस तरह का परीक्षण:

describe('donation controller', function() { 
var $scope, q, window, controller; 

beforeEach(module('giving.donation')); 
beforeEach(inject(function ($controller, $rootScope, $q) { 
    $scope = $rootScope.$new(); 
    window = {}; 
    q = $q; 
    giftFactory = {}; 
    controller = $controller('DonationController', { $scope: $scope, $window: window, GiftFactory: giftFactory }); 
})); 

describe('submit', function() { 
    var deferred; 
    beforeEach(function() { 
     deferred = q.defer(); 

     deferred.promise.success = function (fn) { 
      deferred.promise.then(
       function (response) { 
        fn(response.data, response.status, response.headers); 
       }); 
      return deferred.promise; 
     }; 
     deferred.promise.error = function (fn) { 
      deferred.promise.then(null, 
       function (response) { 
        fn(response.data, response.status, response.headers); 
       }); 
      return deferred.promise; 
     }; 
    }); 

    it('should redirect on success', function() { 
     //Arrange 
     Giving = { endpoints: { indexUrl: "/testurl" } }; 

     giftFactory.createGift = function() { 
      return deferred.promise; 
     }; 

     //Act 
     $scope.submit(); 

     deferred.resolve({}); 
     $scope.$apply(); 

     //Assert 
     expect($scope.submitting).toBeTruthy(); 
     expect(window.location).toBe('/testurl'); 
    }); 

    it('should set submitting back to false on error', function() { 
     //Arrange 
     Giving = { endpoints: { indexUrl: "/testurl" } }; 

     giftFactory.createGift = function() { 
      return deferred.promise; 
     }; 

     //Act 
     $scope.submit(); 

     deferred.reject({}); 
     $scope.$apply(); 

     //Assert 
     expect($scope.submitting).toBeFalsy(); 
     expect(window.location).toBeUndefined(); 
    }); 
}); 

}); 
2

हो सकता है कि देर से लेकिन ... पल कोणीय $ http अनुरोध पर पर एक छोटे से आप एक आधार दे, तो आप इसे करने के लिए बदल सकते हैं:

$http({ 
    method: "yourMethod", 
    url: "yourUrl", 
    data: {yourDataObj} 
}).then(function (res) {  //first function "success" 
    console.log(res.data); 
}, function (err) {   //second function "error" 
    console.log(err); 
}); 
संबंधित मुद्दे