2017-04-03 1 views
0

GetTest 함수가 값을 반환하면 모든 것이 잘 작동하고 그렇지 않으면 오류가 발생합니다. 빈 약속을 어떻게 적절하게 처리 할 수 ​​있습니까?빈 약속을 처리하는 방법

자원을로드하지 못했습니다 : 나는이 시도하지만 난있어 서버가 500 (내부 서버 오류) 상태로 응답

getTest = function (testId) 
{ 
    var promise = $http(
    { 
     method: 'POST', 
     url: self.baseUrl + 'Test/getTest', 
     contentType: 'application/json', 
     data: { 
      testId: testId 
     } 
    }); 
    return promise; 
} 

[HttpPost] 
public ActionResult getTest(string testId) 
{ 
    JsonResult jsonResult = new JsonResult(); 
    try 
    { 
     Test model = new Test(); 
     string returnValue = model.GetTest(Convert.ToInt32(testId)); 
     if(!string.IsNullOrEmpty(returnValue)) 
     { 
      jsonResult.Data = returnValue; 
      Response.StatusCode = 200; 
      return jsonResult; 
     } 
     else 
     { 
      Response.StatusCode = 500; 
      return Content("NoResult"); 
     } 
    } 
    catch (Exception ex) 
    { 
     return jsonResult; 
    } 
} 
+0

"빈 약속"은 무엇을 의미합니까? 오류로 취급 될 상태 코드 500 ("서버 오류")으로 응답하고 있습니다. 오류를 어떻게 처리 할 수 ​​있는지 묻고 있습니까? – JLRishe

답변

2

당신은 두 번째 인수를 사용하여 약속을 실패 처리 할 수 ​​있습니다 .then() :

getTest() 
    .then(
     function (result) { 
      // handle result 
     }, 
     function (error) { 
      // request failed with error 
     } 
    ); 
관련 문제