Parameters are not getting values in POST request using ASMX WebService, Angular 4, Ionic 3


#1

Hi Everyone,

I’m having some problems sending an HTTP POST request with Angular 4 using ASMX WebService. The request arrives to the HTTP server, but apparently service finds no parameters. Here is the code:

code in config.ts

export let SERVER_URL = 'http://abc.com/mobileservice/WebService.asmx/';

code in Headers.ts

getXMLHeader(): Headers {    
        let headers = new Headers();
        headers.append('Content-Type','application/xml');
        headers.append('Access-Control-Allow-Origin', '*');
        headers.append('Access-Control-Allow-Credentials', 'true');
        headers.append('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE , OPTIONS');
        headers.append('Access-Control-Allow-Headers', 'Content-Type, ');        
        return headers;    
      }

code in Provider.ts

insertNewUser(data): Observable<any> {
    this.END_POINT = SERVER_URL + 'InsertNewUser';
    console.log(JSON.stringify(data));
    return this.http.post(`${this.END_POINT}`, data, { headers: this.headers.getXMLHeader() });
  }

code in home.ts

user = {
    SchoolName: '', FirstName: '', LastName: '', SchoolPassword: '', SchoolEmail: '', SchoolAddress: '', SchoolLogo: '',
    SchoolPhoneNumber: '', SchoolGuid: '', AccountAcitve: '', SchoolCode: '', BusinessTypeId: ''
  };

constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController, public operations: OperationsProvider, public loader: LoaderProvider) {
  }

addnewuser() {
   this.operations.insertNewUser(this.user).subscribe(res => {
        console.log(res);
      }, error => {
        alert(ERROR);
      });
    }

And on submitting data i’m getting this error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

System.InvalidOperationException: Request format is invalid: application/xml.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

and no value is being sent to the service parameters.
So I’m not sure if I’m passing the parameters to the server the right way. It should receive the value of post parameters, named SchoolName, FirstName, LastName, SchoolPassword etc.
but it doesn’t.

Thanks in advance!


#2

Add you header to the RequestOptions, like this:

let options = new RequestOptions({ headers: this.headers.getXMLHeader()});
this.http.post(this.END_POINT, data, options)

#3

Sorry Sir, still the smae error i’m getting after implementing your given sol.


#4

Coud it be that your service want xlm data and you send json data to the server?


#5

i resolved the issue by doing code on provider.ts in this way

this.END_POINT = SERVER_URL + 'InsertNewUser';
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = new RequestOptions({ headers: headers });
    let body = 'SchoolName=' + data.SchoolName + '&FirstName=' + data.FirstName + '&LastName=' + data.LastName + '&SchoolPassword=' + data.SchoolPassword +
               '&SchoolEmail=' + data.SchoolEmail + '&SchoolAddress=' + data.SchoolAddress + '&SchoolLogo=' + data.SchoolLogo + '&SchoolPhoneNumber=' + data.SchoolPhoneNumber +
               '&SchoolGuid=' + data.SchoolGuid + '&AccountAcitve=' + false + '&SchoolCode=' + data.SchoolCode + '&BusinessTypeId=' + 1;
    return this.http.post(`${this.END_POINT}`, body, options);