Http Post AngularJS to Ionic v2


#1

I am new to Ionic 2 and am trying to migrate an old application made in v1 now to v2. I’m trying to do a POST to a service developed in C #, specifically WCF, but I get an error in doing so and it’s that the data types I send are not the ones the service receives (JSON or XML). Similar error occurred when I started with ionic v1 that was solved like this:

$http({
         method: 'POST',
         url: url,
         data: JSON.stringify({ dato: 11 }),
         dataType: "json",
         contentType: "application/x-www-form-urlencoded"
})

But I get the same error I received earlier. The service if it receives data of type JSON since it is working correctly in the version with AngularJS

    let data = JSON.stringify({ dato: 11 });
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
    let options = new RequestOptions({ headers: headers });
    this.http.post(url, data, options)
    .subscribe(data => {
      console.log(data);
    }, error => {
      console.log("Error");
    });

#2

I’m baffled as to what content type the backend wants. You say you’re giving it www-form-urlencoded, but then seem to be feeding it JSON.


#3

Yes, but why in AngularJS does it work? If I set it to receive only JSON also fails.


#4

If you set your backend to receive JSON, the following should work:

this.http.post(url, {dato: 11})

#5

not working, Change the content type to application / json and my service will configure it so that it would receive JSON as request data and it will not work either. But since postman YES


#6

You do not have to be so verbose with Angular’s Http client. You don’t need to explicitly set content types. You do not need to manually stringify things. I would concentrate my effort on fixing the backend to be able to handle JSON, as that will pay off for all your other requests.


#7

I think the error is not on the side of the backend, because with Angularjs if it works accordingly.


#8

Good luck with that, then.


#9

I retract !, the problem was the backend. I do not understand why, but I had to leave a STREAM object as a request parameter and then deserialize it in the WebService. I stay with the doubt because in AngularJS worked but with Angular2 not. :stuck_out_tongue: