2016-08-13 50 views
0

我嘗試從南希錯誤信息返回到基於角2Angular2和Nancy:處理錯誤消息

我南希服務器代碼的Web應用程序:

private dynamic GetSolution(dynamic arg) 
    { 
     string solutionName = arg.name; 

     if (solutionName == "error") 
     { 
      var response = Json(new {message = $"error"}); 
      response.StatusCode = HttpStatusCode.BadRequest; 
      return response; 
     } 

     return Json(solutionService.GetSolutionByName(solutionName)); 
    } 

在客戶端與接下來的工序響應代碼:

onButtonClicked(name: string): void 
{ 
    this 
     .solutionsService 
     .getSolutionByName(name) 
     .subscribe(
      p=>this.showMessage(p.title), 
      p=>this.showError(p.message)); 
    //this.refresh(true); 
} 

如果效應初探與狀態200 - 我接收預期的對象(字段ID和標題的溶液),enter image description here

但如果狀態碼0錯誤(400例)我得到意外的對象: enter image description here

我該怎麼辦了?

回答

0

錯誤發生在CORS問題中。在成功的響應中,我添加了CORS標題來響應,但在無效響應中,我沒有這樣做。那就是爲什麼Angular回覆OPTIONS在飛行前請求中的迴應。