Cordova (IONIC) http post does not work even after including header details

I am using Ionic 4 to create a small mobile app. The back-end is C# WebAPI. I need to submit some data to the server. And it does not work.

The Code:

NewsDetails = {
    "ServerID": "1",
    "Title": "Test",
    "Source": "Self",
    "Body": "Hello world"
  };
  const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
        })
      };

    this.http.post("https://sitename.com/DataServer/News", this.NewsDetails, httpOptions)
      .subscribe(data => {
        this.router.navigateByUrl("/home/news");
      }, (err) => {
        console.error("unable to connect to server:", JSON.stringify(err));
      });

The above code produces the following error:

{
 "headers":{"normalizedNames":{},
 "lazyUpdate":null,
 "headers":{}},
 "status":0,
 "statusText":"Unknown Error",
 "url":"https://sitename.com/DataServer/News",
 "ok":false,
 "name":"HttpErrorResponse",
 "message":"Http failure response for https://sitename.com/DataServer/News: 0 Unknown Error",
 "error":{"isTrusted":true}
}

I tried updating the same from POSTMAN application and works fine. Any idea the reason for the error?

Thanking u in advance.

1 answer

  • answered 2020-04-03 07:31 Tomislav Stankovic

    Android 8: Cleartext HTTP traffic not permitted

    Inside config.xml you can add:

    <application android:usesCleartextTraffic="true" />
    

    like this:

    <platform name="android">
    ...
       <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
             <application android:usesCleartextTraffic="true" />
       ...
       </edit-config>