【问题标题】:Angular6 http post method does not pass data to web apiAngular6 http post方法不会将数据传递给web api
【发布时间】:2018-09-15 05:38:26
【问题描述】:

我没有从 Angular 6 应用程序接收到关于 asp.net mvc core api 的数据。 图片在这里 MVC core api

httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json'
    })
  };  

  createUser(user: User): Observable<any> {;
    let body = JSON.stringify(user);
    return this.http.post<any>('http://localhost:51001/api/User/' + "PostUser", body, this.httpOptions)
      .pipe(catchError(this.handleError));
  }

【问题讨论】:

  • 向我们展示代码! 在此处发布相关的 Angular 和 Web API 代码 - 作为文本,格式正确。
  • 请注意,不需要添加 Content-Type 标头,因为 Angular 会为您添加此标头

标签: c# .net angular asp.net-core-mvc


【解决方案1】:

不要调用 JSON.stringify,因为 Angular Http / HttpClient 类会为您执行此操作。如果你自己做,Angular 会在字符串上调用stringify,这会导致你看到的问题。

  createUser(user: User): Observable<any> {;
    return this.http.post<any>('http://localhost:51001/api/User/' + "PostUser", user, this.httpOptions)
      .pipe(catchError(this.handleError));
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    相关资源
    最近更新 更多