简体   繁体   中英

POST http calls not working in Ionic iOS build

I've made an application in Ionic V4 and then build the same using cordova plugin (not capacitor). First page of my application is login where I'm getting values from form fields and sending it to login method where I am making a POST call to server.

loginUrl = 'http://localhost:3000/login';
errorLog: any;

login(form) {
   const user = form.values;
   this.httpClient.post(this.loginUrl, user).subscribe( response => {
      this.toastController.create({message:'Login Successful', duration: 1000}).then(toast=> toast.present());
   }, error => {
      this.errorLog = error;
      this.toastController.create({message:'Some Error Occured', duration: 1000}).then(toast=> toast.present());
   })
} 

Also I'm printing that error log in login page because once we make build, its hard to debug in device using Android Studio or Xcode .

So I'm printing error log like this

<h3>{{errorLog | json}}</h3>

I'm not getting error from POST call in case of running the application in web browser as well as in android. Getting error in iOS when I tried to make build and using Xcode , deployed into my iPad.

It's working fine in GET HTTP calls because previously I've tested GET HTTP calls in same but not working in POST HTTP calls.

Its returning 500 internal server error. Error object looks like

{
   "statusText": "OK",
   "status": 500,
   "url": "http://localhost:3000/login",
   "ok": false,
   "name": 'HttpErrorResponse',
   "message": "Http failure response for http://localhost:3000/login: 500 OK",
   ...
}

How I make build is

  1. ionic build --prod --release
  2. ionic cordova platform remove ios (if platform already exist).
  3. ionic cordova platform add ios
  4. ionic cordova prepare ios

Then I open the same ios folder into Xcode, select the developer account, connect my iPad and run it.

Were you able to resolve it? I faced similar situation and here's how I got around it. In capacitor.config.json file we need to specify 'server' from where we are getting the Api data.

 "appId": "com.yoursite",
 "appName": "Your Site",
 "bundledWebRuntime": false,
 "npmClient": "npm",
 "webDir": "www",
 "cordova": {},

//This part mentioned below is important.
 "server": {
   "url": "https://yoursite.com",
   "allowNavigation": ["yoursite.com", "*.yoursite.com"]
 },

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM