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