![](/img/trans.png)
[英]Javascript Code not working in Chrome, but works everywhere else
[英]Why AJAX call is not working only on iOS, everywhere else it works great?
我正在使用jQuery和PHP从零开始创建一些AJAX星级评级,并且已经意识到它在任何地方工作正常但在iOS上没有。 我检查了一些插件(jRating)并遇到了同样的问题。 它将数据发送到服务器(PHP)但在错误:handler处返回Error Undefined。
我已经尝试了几乎所有其他平台和浏览器(Linux - chrome,FF,Opera; Windows - IE,Edge; Android - Chrome,Opera),并且每个人都按预期工作。 iOS上没有浏览器正在运行。 值得一提的是服务器落后于Amazon Cloudfront。 但我让它从原始服务器传递所有Headers,我可以在Chrome Developer Console中看到正确的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);
它应该写成功但它会警告Rate问题。 错误:udnefined。 textStatus:error,errorThrown:
我相信你在本地计算机上进行测试。
当您在计算机上测试时,“my / ajax / url”指的是同一个域。 因此,假设您的测试网站位于http://localhost/mywebsite
,AJAX将点击完整的绝对URL,即“ http:// localhost / mywebsite / my / ajax / url ”
在你的iOS上(我相信它是一个不同的设备,如iPhone /平板电脑/笔记本电脑/等),当AJAX尝试点击“ http:// localhost / mywebsite / my / ajax / url ”时,它正试图在相同的设备 (iPhone /平板电脑/笔记本电脑/等),而不是托管网站的计算机。
所以:
echo "hi from server"; exit;
echo "hi from server"; exit;
,并在你的AJAX成功函数中,把这个: alert(data);
检查您是否将AJAX发送到正确的URL(#1),以及从正确的服务器(#2)接收。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.