以下代码适用于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);
答案 0 :(得分:0)
尝试在您的http请求中添加此行
标题:{'内容类型':' application / x-www-form-urlencoded' }