No error returned but no data returned on successful json post to api

Hi all,

I am attempting a json post to an api expected to return json data in the format [‘token’: ‘’, status: ‘’, error: ‘’] but no data is returned.

My sample code is :

import { Component } from '@angular/core';
import { HTTP } from '@ionic-native/http/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

    public amessage = '';
    public bmessage = '';

    public errmessage: boolean = false;

  constructor(private http: HTTP) {}

  getVehicles() {

    this.http.setDataSerializer('json');

    this.http.post(
      "http://api.*********.com/WebProcessorApi.ashx",             //URL
      {
        "Token":"", 
        "OperationType":"SignIn", 
        "InformationType":"User", 
        "LanguageType":"", 
        "Arguments": {"UserName":"username","Password":"password"}

      },         //Data,
      { "Content-Type": "application/json", "Access-Control-Allow-Origin" : "*"} //Headers
     )
     .then(response => {
        // prints 200
        console.log(response.status);
        try {
          this.amessage = JSON.stringify(response);
          // prints test
          console.log(response);
          //this.amessage = response;
        } catch(e) {
          console.error('JSON parsing error');
        }
     })
     .catch(response => {
         this.errmessage = true;
       // prints 403
       console.log(response.status);
        this.bmessage = response.error;
       // prints Permission denied
       console.log(response.error);
     });

  }
}

The value returned is

There was an instance when it did return the proper expected data json format but only when it was displaying a returned the error.

It returned [‘token’: ‘’, status: ‘’, error: ‘Object reference not set to an instance of an object in web api’] but that was before I added

this.http.setDataSerializer('json');

after i added that line, no data is returned.

what could be the possible cause?

Thanks

Please see if your problem persists when using the Angular HttpClient and an HTTPS endpoint.

Hi @rapropos, I had tried with HttpClient but I kept getting CORS errors regarding existing headers and it was suggested that I use ionic native HTTP

Well, that doesn’t seem to be working so great for you. My suggestion would be that CORS needs to be fixed on the server, and once that’s done you can go back to using Angular HttpClient, which is much easier to deal with.

Alas, I have no access to the server. I sent a inquiry to the server admin regarding CORS but till i hear back from them it back to trial and error my end :expressionless:

If all else fails, you can contemplate remedying that by inserting a proxy that you do have access to into the equation.