我如何发送请求Angular 5?

时间:2018-03-26 14:35:21

标签: angular typescript ionic-framework ionic3

我有发送请求的下一个功能

addSite(address, username, password, device_token, os_type){
    this.headers = (new HttpHeaders())
      .set('Content-type', 'application/json')
      .set('Access-Control-Allow-Method', 'POST');

    let params = new HttpParams();
    params.set('username', username)
          .set('password', password)
          .set('device_token', device_token)
          .set('os_type', os_type);
    return this.http.post(`${address + Config.API_URL}, params,
      {headers: this.headers} )
  }
}

我总是得到错误http://joxi.net/52aXvbVtGpReqm,当我在没有参数的情况下做请求时就发生了这种情况。但我有参考,请帮我找错。

1 个答案:

答案 0 :(得分:-1)

将param作为此类

的表单数据发送
addSite(address, username, password, device_token, os_type){
    this.headers = (new HttpHeaders())
      .set('Content-type', 'application/json')
      .set('Access-Control-Allow-Method', 'POST');

    const formData = new FormData();
    formData.append('username', username);
    formData.append('password', password);
    formData.append('device_token', device_token);
    formData.append('os_type', os_type);
    return this.http.post(`${address + Config.API_URL}, formData,
      {headers: this.headers} )
  }
}