Angular 2/Web Api-json解析错误语法错误输入意外结束 [英] Angular 2/Web Api - json parsing error syntax error unexpected end of input
问题描述
我有一个如下所示的Web API控制器:
I have a Web API controller that looks like this:
[HttpPost]
public IHttpActionResult Test()
{
return Ok();
}
这是正确的语法,但是当我尝试从Angular 2的服务中调用它时,出现错误消息:"json解析错误语法错误,输入意外结束".要解决此问题,我必须在ActionResult中添加一个值,例如
This is correct syntax, However when I try to call this from a service in Angular 2, I get the error message: "json parsing error syntax error unexpected end of input." To resolve this issue, I have to put a value into the ActionResult such as
return Ok(1)
我缺少一些配置吗?我的Angular 2服务电话如下:
Am I missing some configuration? My Angular 2 service call looks like this:
return this.http.post(API/Controller/Test).map(res => res.json());
推荐答案
我猜想,当您收到一个空响应(没有有效负载)时,不需要调用json
方法.在幕后,XHR响应未定义,调用JSON.parse(undefined)
并引发错误.
I guess that when you receive an empty response (without payload) you don't need to call the json
method. Under the hood, the XHR response is undefined, JSON.parse(undefined)
is called and an error is thrown.
您可以跳过map
运算符的调用:
You could skip the call of the map
operator:
return this.http.post(API/Controller/Test)/*.map(res => res.json())*/;
这篇关于Angular 2/Web Api-json解析错误语法错误输入意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!