我正在使用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网址配置为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)
当我尝试运行Project时会发生这种情况。
我在这里缺少什么?