【发布时间】: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