C# NodeInvocationException:JSON输入意外结束
我正在使用Asp.Net内核和Angular 4作为前端 我打电话给ProjectRoleService如下C# NodeInvocationException:JSON输入意外结束,c#,angular,asp.net-core-2.0,C#,Angular,Asp.net Core 2.0,我正在使用Asp.Net内核和Angular 4作为前端 我打电话给ProjectRoleService如下 import { Injectable, Inject } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { Http } from '@angular/http'; import 'rxjs/Rx'; @Injectable() export class ProjectRoleSe
import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class ProjectRoleService {
_serverURL: string;
constructor(private _http: Http) {
this._serverURL = "http://localhost:8080/api";
}
getMenuDetails(roleName: string) {
let _url = this._serverURL + "/Menu/GetMenuDetails?roleName=" + roleName;
return new Promise((resolve, reject) => {
this._http.get(_url)
.map(res => res.json())
.catch((error: any) => {
console.error(error);
reject(error);
return Observable.throw(error.json().error || 'Server error');
})
.subscribe((data) => {
resolve(data);
});
});
}
}
public class MenuController : ApiController
{
/// <summary>
/// Get Menu Details by logged in user role
/// </summary>
/// <param name="email ID"></param>
/// <returns></returns>
[Microsoft.AspNetCore.Mvc.HttpGet]
public HttpResponseMessage GetMenuDetails(string roleName)
{
HttpResponseMessage httpResponseMessage = null;
try
{
httpResponseMessage = Request.CreateResponse(new Menu().GetMenuDetails(roleName));
}
catch (Exception ex)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(ex.Message),
ReasonPhrase = "Warning"
});
}
return httpResponseMessage;
}
}
我已将本地主机URL配置为http://localhost:8080/
并将服务器URL设置为this.\u serverURL=”http://localhost:8080/api";代码>
MenuController
类如下
import { Injectable, Inject } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class ProjectRoleService {
_serverURL: string;
constructor(private _http: Http) {
this._serverURL = "http://localhost:8080/api";
}
getMenuDetails(roleName: string) {
let _url = this._serverURL + "/Menu/GetMenuDetails?roleName=" + roleName;
return new Promise((resolve, reject) => {
this._http.get(_url)
.map(res => res.json())
.catch((error: any) => {
console.error(error);
reject(error);
return Observable.throw(error.json().error || 'Server error');
})
.subscribe((data) => {
resolve(data);
});
});
}
}
public class MenuController : ApiController
{
/// <summary>
/// Get Menu Details by logged in user role
/// </summary>
/// <param name="email ID"></param>
/// <returns></returns>
[Microsoft.AspNetCore.Mvc.HttpGet]
public HttpResponseMessage GetMenuDetails(string roleName)
{
HttpResponseMessage httpResponseMessage = null;
try
{
httpResponseMessage = Request.CreateResponse(new Menu().GetMenuDetails(roleName));
}
catch (Exception ex)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(ex.Message),
ReasonPhrase = "Warning"
});
}
return httpResponseMessage;
}
}
public类菜单控制器:ApiController
{
///
///通过登录用户角色获取菜单详细信息
///
///
///
[Microsoft.AspNetCore.Mvc.HttpGet]
公共HttpResponseMessageGetMenuDetails(字符串roleName)
{
HttpResponseMessage HttpResponseMessage=null;
尝试
{
httpResponseMessage=Request.CreateResponse(新菜单().GetMenuDetails(roleName));
}
捕获(例外情况除外)
{
抛出新的HttpResponseException(新的HttpResponseMessage(HttpStatusCode.InternalServerError)
{
内容=新的字符串内容(例如消息),
ReasonPhrase=“警告”
});
}
返回httpResponseMessage;
}
}
在IISExpress上运行时,我得到如下错误。我在项目设置中仅为此启用了匿名
NodeInvocationException: Unexpected end of JSON input
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.module.exports.Body.json (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:71887:25)
at CatchSubscriber.selector (E:\AngularProject\NodeUI\ClientApp\dist\main-server.js:19325:96)
at CatchSubscriber.module.exports.CatchSubscriber.error (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:91048:31)
at MapSubscriber.module.exports.Subscriber._error (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:10724:26)
at MapSubscriber.module.exports.Subscriber.error (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:10698:18)
at ZoneTask.onComplete [as callback] (E:\AngularProject\NodeUI\ClientApp\dist\main-server.js:4454:30)
at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:85819:31)
at Object.onInvokeTask (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:14821:37)
at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:85818:36)
NodeInvocationException:JSON输入意外结束
SyntaxError:JSON输入意外结束
在JSON.parse()处
位于Response.module.exports.Body.json(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:71887:25)
在CatchSubscriber.selector(E:\AngularProject\NodeUI\ClientApp\dist\main server.js:19325:96)
位于CatchSubscriber.module.exports.CatchSubscriber.error(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:91048:31)
在MapSubscriber.module.exports.Subscriber.\u错误(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:10724:26)
在MapSubscriber.module.exports.Subscriber.error(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:10698:18)
在ZoneTask.onComplete[作为回调](E:\AngularProject\NodeUI\ClientApp\dist\main server.js:4454:30)
位于ZoneDelegate.module.exports.ZoneDelegate.invokeTask(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:85819:31)
位于Object.onInvokeTask(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:14821:37)
位于ZoneDelegate.module.exports.ZoneDelegate.invokeTask(E:\AngularProject\NodeUI\ClientApp\dist\vendor.js:85818:36)
当我尝试运行项目时,就会发生这种情况
我怀疑问题在于调用服务器URL
我的res.json()足够好吗
我在这里遗漏了什么?“JSON输入的意外结束”表示出现了一些格式错误的JSON。您应该查看服务器响应并确保它实际上是JSON。当出现异常时,您的响应中没有JSON。.JSON()
也会返回一个承诺。我如何检查它是否返回JSON数据?是否有条件检查内容类型?