http post request not working inside ionic app, but the same post request works in postman

178 Views Asked by At

I am trying to make an app in ionic v3 inside visual studio (Tools for apache cordova). In one of the app screen, I take information from user and send it to the api. The problem is the http post request that I make is not working correctly. If I make the same request from postman, it works and not from my app page

I have tried all answers I could find none helped me. I have tried add change header that i pass with request (passing'user-agent')

 let header = new Headers({
   'Content-Type': 'application/json; charset=utf-8',
   'Authorization': 'console',
   'MacAddress': localStorage.getItem('macAddress'),
   'Key': localStorage.getItem('key'),
   'access-control-allow-origin': '*',
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
 });
 let data = JSON.stringify({
   FirstName: this.FirstName,
   LastName: this.LastName,
   MI: this.u_mname,
   Address: this.Address,
   City: this.City,
   State: this.State,
   ZipCode: this.ZipCode,
   HomePhone: this.HomePhone,
   OfficePhone: this.OfficePhone,
   OfficePhoneExt: this.OfficePhoneExt,
   CellPhone: this.CellPhone,
   Fax: this.Fax,
   Email1: this.Email1,
   Email2: this.Email2,
   ContactTypeId: this.ContactTypeId,
   dob: this.dob,
   ApplicationId: this.ApplicationId
 });

 this.http.post(localStorage.getItem('base_url') + '/services/odata/tblContacts?Mother=0&Father=0&Guardian=0&groupIdList=0', data, { headers: header})
   .map(res => console.log(res))
   .subscribe(
      data => {
       alert(data)
     }, err => {
       alert(err)
     })

I expected response should be 'http status code 201', but I am getting status code 200 and message 'Internal Server Error. Please see detail on server logs.'

1

There are 1 best solutions below

0
Yash Rami On

You can try like these.

public httpOptions;

this.httpOptions = {
      headers: new HttpHeaders({ 
   'Content-Type': 'application/json; charset=utf-8',
   'Authorization': 'console',
   'MacAddress': localStorage.getItem('macAddress'),
   'Key': localStorage.getItem('key'),
   'access-control-allow-origin': '*',
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
   })
    };

 let data = JSON.stringify({
   FirstName: this.FirstName,
   LastName: this.LastName,
   MI: this.u_mname,
   Address: this.Address,
   City: this.City,
   State: this.State,
   ZipCode: this.ZipCode,
   HomePhone: this.HomePhone,
   OfficePhone: this.OfficePhone,
   OfficePhoneExt: this.OfficePhoneExt,
   CellPhone: this.CellPhone,
   Fax: this.Fax,
   Email1: this.Email1,
   Email2: this.Email2,
   ContactTypeId: this.ContactTypeId,
   dob: this.dob,
   ApplicationId: this.ApplicationId
 });

 this.http.post(
  localStorage.getItem('base_url') + '/services/odata/tblContacts?Mother=0&Father=0&Guardian=0&groupIdList=0', 
data,
httpOptions
}).map(res => console.log(res))
  .subscribe(
     data => {
       alert(data)
     }, err => {
       alert(err)
     })