2016-08-13 90 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在飞行前请求中的回应。