c# - NodeInvocationException:JSON 输入意外结束
问题描述
我正在使用 Asp.Net Core 和 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 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);
});
});
}
}
我已将 localhost URL 配置为http://localhost:8080/
,将 serverURL 配置为this._serverURL = "http://localhost:8080/api";
班级MenuController
如下
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;
}
}
有了这个,我在从 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)
当我尝试运行项目时会发生这种情况。
- 我怀疑问题出在调用服务器 URL
- 我的 res.json() 是否足够好?
我在这里想念什么?
解决方案
推荐阅读
- pandas - 如何在 Pandas 数据框中搜索 dtype: object 的特定日期(2010-12-01)?
- python - 如何在 Python 中将女性和男性更改为 True 和 False
- android - 致命异常:main:PID:32439 java.lang.NullPointerException,我是 android 新手。请帮我解决这个问题,
- flutter - 我们可以在不使用 package_info 或 Flutter 中的任何其他依赖项的情况下获取应用程序版本名称吗?
- bigcommerce - BigCommerce 应用程序应如何使用第 3 方 API 进行身份验证?
- woocommerce - 在 WooCommerce 特色产品 Gutenberg 块中的描述中添加自定义字段
- swift - 在 NSGridView 中显示可编辑的 NSTextField
- google-cloud-firestore - react-quilljs 返回值是对象。如何将其保存到firestore
- node.js - 在同一个域(cpanel)上托管 angular 和 nodejs
- firebase - 在flutter firebase中调用了方法'[]'的空错误