【问题标题】:SyntaxError: Illegal return statement in Angular applicationSyntaxError:Angular 应用程序中的非法返回语句
【发布时间】:2018-01-19 07:36:08
【问题描述】:

在最后一行代码(return 语句)中运行以下代码时,我收到 SyntaxError: Illegal return statement。在执行最后一行之后,我得到 TypeError: Cannot read property 'post' of undefined 错误。

constructor(private _http:Http);
createCategory(vm: Category): Observable<any>{  
vm.Id = 25;
var body = JSON.stringify(vm);
var headerOptions = new Headers({ 'Content-Type': 'application/json' });   
return this._http.post('http://localhost:55861/api/CategoryApi', body, {headers: headerOptions }).map(x => x.json());
} 

我正在 Visual Studio 中运行 Angular 项目。上面的代码用于将数据发布到 web api POST 操作方法。谁能帮帮我?..

【问题讨论】:

  • 你能展示全班吗?或者至少是构造函数
  • 好像班上不知道this._http是什么,你在注入吗?
  • 是的..我正在注射它。

标签: angular asp.net-web-api


【解决方案1】:

将 content-type 更改为 application/x-www-form-urlencoded 后,我的主要问题解决了...即。在调试时,可以调用WebAPI的Post动作方法(即编译器在运行时会转到API Post方法,这在之前是没有的)。

var headerOptions = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    var requestOptions = new RequestOptions({ method: RequestMethod.Post, headers: headerOptions });
    return this._http.post('http://localhost:55861/api/CategoryApi/Post', body, requestOptions).map(x => x.json());

但现在的问题是,下面的行应该怎么办?.. 由于我将 content-type 更改为 json 类型以外的 并且来自客户端的数据是 null(在 API 的 POST 中)就目前而言。任何人都可以帮我解决这个问题..在此先感谢..

body, requestOptions).map(x => x.json());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多