繁体   English   中英

angular ionic http request sends null data to rest API

[英]angular ionic http request sends null data to rest API

我正在尝试将数据从 angular 表单发送到端点。 此 angular 表单位于 ionic 4 应用程序中。 但我得到的只是后端的 null 数据。 但是当我尝试对 postman 进行相同操作时,它工作正常。我也尝试将内容类型更改为“application/json”,但它也给出了相同的结果。 对于后端,我使用的是 php api。

// Http Options,where I set http headers
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
  };


// Create a new user
  createUser(user: User): Observable<User> {
    return this.http
      .post<User>(this.url + 'test/create' , user , this.httpOptions)
      .pipe(
        retry(0),
         // handle error method is already implemented above     
        catchError(this.handleError)
      );
  }
}


I'm not receiving any errors in chrome console.

您需要将数据作为表单数据传递,如下所示:

const newUser = new FormData(); 
newUser.append('firstName', user.firstName);

然后在您的帖子中传递 newUser。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM