首页 > 解决方案 > 角度离子http请求将空数据发送到rest API

问题描述

我正在尝试将数据从角度形式发送到端点。这种角度形式在 ionic 4 应用程序中。但我得到的只是后端的空数据。但是当我尝试使用邮递员时,它工作正常。我也尝试将内容类型更改为“应用程序/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.

标签: angulartypescriptionic4

解决方案


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

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

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


推荐阅读