【问题标题】:$http not working in iOS 8.2 in Ionic / AngularJS app$http 在 Ionic / AngularJS 应用程序的 iOS 8.2 中不起作用
【发布时间】:2017-10-31 17:39:45
【问题描述】:

以下代码适用于 iOS 9.3++ 的 Ionic 应用程序,但不适用于 iOS 8.2:

  $http({
      method: 'POST',
      url: 'https://example.com',
      data: {username: username, password: password }
  }).then(function(result) {
     //I wish!
  },function(error) {
    //unfortunately we end up here for iOS 8.2, but not for 9.3 or greater
  });

我已验证两个版本都将 POST 数据发送到服务器,并且服务器会生成正确的响应。

iOS 8.3 抛出错误:

Failed to load resource: The network connection was lost.

我怀疑某种浏览器设置/权限/CORS 问题,但在 SO 或 google 上没有找到解决方案。

我的服务器 (PHP) 设置为接收这样的 ajax 请求:

if (isset($_SERVER['HTTP_ORIGIN'])) {
   header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
   header('Access-Control-Allow-Credentials: true');
   header('Access-Control-Max-Age: 86400');
}

//Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
   if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {      
       header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
   }
   if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
      header("Access-Control-Allow-Headers:        
      {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
   } 
   exit(0); 
}

//processing code goes here and a proper $response is created
echo json_encode($response);

【问题讨论】:

  • 尝试在标题中设置内容类型。例如Content-Type: application/json.. 我之前遇到过类似的错误,这是经过数小时调试后的问题.. 希望对您有所帮助!
  • 设置为:Content-Type application/json;charset=UTF-8

标签: php ios angularjs cordova ionic-framework


【解决方案1】:

尝试在您的 http 请求中添加这一行

标题:{ 'Content-Type': 'application/x-www-form-urlencoded' }

【讨论】:

  • 这会破坏服务器(400 错误),它需要 JSON。同样,该代码在 iOS 9,10、android 和浏览器上都能完美运行。
  • 你能给我你的科尔多瓦版本吗
  • 删除 cordova-plugin-whitelist 并添加回白名单并将 添加到您的 config.xml
  • 科尔多瓦 v6.4。我卸载并重新安装了白名单插件,并将该行添加到 config.xml。我已经有href="*"。没用。
猜你喜欢
  • 2019-07-26
  • 1970-01-01
  • 2019-06-05
  • 1970-01-01
  • 2022-01-14
  • 2023-03-11
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
相关资源
最近更新 更多