【发布时间】:2019-10-27 19:58:12
【问题描述】:
我正在使用 jQuery 和 PHP 从头开始创建一些 AJAX 星级评级,并意识到它在任何地方都可以正常工作,但在 iOS 上却不行。我检查了一些插件(jRating)并遇到了同样的问题。它将数据发送到服务器(PHP),但在错误时返回错误未定义:处理程序。
我已经尝试了几乎所有其他平台和浏览器(Linux - chrome、FF、Opera;Windows - IE、Edge;Android - Chrome、Opera)并且一切都按预期工作。 iOS上的浏览器都没有工作。值得一提的是,该服务器位于 Amazon Cloudfront 之后。但是我让它传递了来自源服务器的所有标头,我可以在 Chrome 开发者控制台中看到正确的 Access-Control-Allow-Origin 标头。
我还搜索并尝试了所有关于缓存和相对 URL、JSON 数据类型等的建议,但没有成功。我还尝试将随机参数添加到 POST URL - 没有运气。
有时,假设每三个请求它能够返回正确的数据。
我的 JS 代码:
$('.mystars.ajax input').on('click', function(e){
e.preventDefault();
var form = $(this).parent();
$.ajax({
type: "POST",
url: 'my/ajax/url',
context: this,
cache: false,
dataType: 'json',
headers: { "cache-control": "no-cache" },
async: true,
crossDomain: true,
data: form.serialize(),
xhrFields: {
withCredentials: true
},
success: function(data)
{
alert('xhr success');
// parse show message to user according to server response.
},
error: function(jqXhr, textStatus, errorThrown) {
alert("Rate problem. Error: " + jqXhr.responseText + ", textStatus: " + textStatus + ", errorThrown: " + errorThrown)
},
complete: function(){
console.log('compete');
}
});
});
这是服务器响应代码的一部分:
$myResponse['error'] = false;
$myResponse['message'] = 'Everything is just fine';
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');
header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS');
}
header('Content-Type: application/json');
echo json_encode($myResponse);
exit(0);
它应该写成功,但它会警告速率问题。错误:未定义。文本状态:错误,错误抛出:
【问题讨论】:
标签: javascript php jquery ios ajax