http request method post Ionic 2, angular 2

Asked

Viewed 232 times

1

I’m making an http post request, but without success...

I actually have a success in the header the same returns, but the body is not rolling.

My code is like this:

          postRequest() {
          let myHeader = new Headers({

        "Content-Type" : "application/x-www-form-urlencoded",


       });
        let options = new RequestOptions({
          headers: myHeader

        });
        let body = JSON.stringify({

          key: "versao" , value:"1.1"


        });

On the body I need a Body request with key and value to return the query made in the webservice.

I saw on some websites that say that the body has to be written in the format "application/x-www-form-urlencoded".

could help ?

1 answer

0

Ronald. You are doing a POST but you are not sending any data to the server. Just below, you say you want to do a web-service query, right? For that, the correct method is GET. Would something like this:

getRequest(): Observable<Response>{
        let myUrl = "http://www.google.com"; //sua url de acesso
        let myHeader = new Headers({'Content-Type': 'application/json'}); // JSON!
        let options = new RequestOptions({headers: myHeader});
        let body = JSON.stringify({
          key: "versao" , value:"1.1"
        });

      return this.http.get(myUrl, body).map(this.handleData);
       // chama a função abaixo
}

private handleData(res: Response) {
  let body = res.text();
  if (body) {
    return body.data;
  } else {
    return {};
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.