简体   繁体   English

POST http 调用在 Ionic iOS 版本中不起作用

[英]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).我在 Ionic V4 中创建了一个应用程序,然后使用cordova插件(不是电容器)构建了相同的应用程序。 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.我的应用程序的第一页是登录,我从表单字段获取值并将其发送到登录方法,在那里我对服务器进行POST调用。

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 .此外,我正在登录页面打印该错误日志,因为一旦我们进行构建,就很难使用 Android Studio 或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.在 Web 浏览器和 android 中运行应用程序的情况下,我不会从POST调用中收到错误。 Getting error in iOS when I tried to make build and using Xcode , deployed into my iPad.当我尝试构建并使用Xcode并部署到我的 iPad 时,在 iOS 中出现错误。

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.它在GET HTTP调用中工作正常,因为之前我已经在相同的情况下测试了GET HTTP调用,但在POST HTTP调用中不起作用。

Its returning 500 internal server error.它返回 500 内部服务器错误。 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). ionic cordova platform remove ios (如果平台已经存在)。
  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.然后我在 Xcode 中打开同一个ios文件夹,选择开发者帐户,连接我的 iPad 并运行它。

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.在capacitor.config.json 文件中,我们需要指定'server' 从中获取Api 数据的位置。

 "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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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