简体   繁体   English

Angular2 - HTTP 200 被视为错误

[英]Angular2 - HTTP 200 treated as error

I am trying to get Angular2 to work with my Asp.Net WebApi 2 server.我试图让 Angular2 与我的 Asp.Net WebApi 2 服务器一起工作。 I managed to handle some GET requests correctly, however this POST request behaves strangely.我设法正确处理了一些 GET 请求,但是这个 POST 请求的行为很奇怪。 I receive an OK (200) response from my server, yet the following code treats it as an error:我从我的服务器收到 OK (200) 响应,但以下代码将其视为错误:

public Register(){
    this.accountService.Register(this.Name, this.Password, this.RepeatPassword, this.Email, this.Skype, this.Website).subscribe(
        () => {      //this is what's supposed to be called, but isn't
            this.accountService.Login(this.Name, this.Password).subscribe(
                res => {
                    console.log(res);
                    localStorage.setItem('token', res);
                    localStorage.setItem('user', this.Name);
                    this.router.navigate(['Home']);
                },
                error2 => {
                    console.log(error2.Message);
                }
            );
        },
        error => { //the response gets here, instead of being handled above
            console.log(error.Message);
        }
    );
}

Here is the Register method of the accountService:这是 accountService 的 Register 方法:

public Register (userName:string, password:string, confirmPassword:string, email:string, skype:string, website:string)
{
    return this.http.post(this.Uri + 'api/Account/Register', JSON.stringify(
        {
            UserName: userName,
            Password: password,
            ConfirmPassword: confirmPassword,
            Email: email,
            Skype: skype,
            Website: website
        }), this.GetRequestOptions() ).map((res: Response) => res.json());
}

Finally found the answer.终于找到了答案。 Ajax, when it's expecting a json, fires off the error callback after getting HTTP 200 with a malformed json (including an empty file).当 Ajax 需要一个 json 时,它会在获取带有格式错误的 json(包括一个空文件)的 HTTP 200 后触发错误回调。 The fix was to replace all empty responses with {}.修复方法是用 {} 替换所有空响应。

To do this in a ASP.Net WebApi2 server, you need to use return Ok("{}");要在 ASP.Net WebApi2 服务器中执行此操作,您需要使用return Ok("{}"); instead of just return Ok();而不是仅仅return Ok(); at the end of the method being called.在被调用的方法结束时。

An alternative solution (and most likely a better one) is to change the calling code - in particular, .map((res: Response) => res.json()) is the bit of code that failed - a conditional checking if res is empty could be added before calling res.json() .另一种解决方案(并且很可能是更好的解决方案)是更改调用代码 - 特别是.map((res: Response) => res.json())是失败的代码位 - 条件检查是否res在调用res.json()之前可以添加为空。

I was having the same problem here.我在这里遇到了同样的问题。 But in my case it was that I forgot to tell Angular which kind of response my endpoint was sending.但就我而言,我忘记告诉 Angular 我的端点正在发送哪种响应。 For more information you can read the GreyBeardedGeek response at stackoverflow .有关更多信息,您可以阅读stackoverflow 上的 GreyBeardedGeek 响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM