2017-04-03 81 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 
     } 
    );