Angular2 和 Nancy:处理错误消息

Angular2 and Nancy: Processing error message

我尝试 return 从 Nancy 错误消息到基于 Angular 2.

的网络应用程序

我的南希服务器代码:

    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 的响应 - 我收到预期的 object(包含字段 ID 和标题的解决方案),

但如果状态代码 0 错误(例如 400),我会感到意外 object:

我做错了什么?

错误出在 CORS 问题上。在成功响应中,我将 CORS headers 添加到响应中,但在无效响应中我没有这样做。这就是为什么 Angular return 对我的响应来自 OPTIONS pre-flight 请求。