Angular 2 http post params and body

前端 未结 5 2033
暖寄归人
暖寄归人 2021-02-02 16:32

I\'m trying to do an api call from my angular app. What I want to do is send a post request to the api with a command param. I have done a lot of server side testing as well as

5条回答
  •  死守一世寂寞
    2021-02-02 16:55

    And it works, thanks @trichetriche. The problem was in my RequestOptions, apparently, you can not pass params or body to the RequestOptions while using the post. Removing one of them gives me an error, removing both and it works. Still no final solution to my problem, but I now have something to work with. Final working code.

    public post(cmd: string, data: string): Observable {
    
        const options = new RequestOptions({
          headers: this.getAuthorizedHeaders(),
          responseType: ResponseContentType.Json,
          withCredentials: false
        });
    
        console.log('Options: ' + JSON.stringify(options));
    
        return this.http.post(this.BASE_URL, JSON.stringify({
          cmd: cmd,
          data: data}), options)
          .map(this.handleData)
          .catch(this.handleError);
      }
    

提交回复
热议问题