首页 > 解决方案 > 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)

当我尝试运行项目时会发生这种情况。

  1. 我怀疑问题出在调用服务器 URL
  2. 我的 res.json() 是否足够好?

我在这里想念什么?

标签: c#angularasp.net-core-2.0

解决方案


推荐阅读