简体   繁体   English

为什么AJAX调用不仅仅适用于iOS,其他任何地方都可以运行得很好?

[英]Why AJAX call is not working only on iOS, everywhere else it works great?

I'm creating some AJAX star rating from scratch using jQuery and PHP and have realised that everywhere it is just working fine but not on iOS. 我正在使用jQuery和PHP从零开始创建一些AJAX星级评级,并且已经意识到它在任何地方工作正常但在iOS上没有。 I checked some plugins (jRating) and has the same problem. 我检查了一些插件(jRating)并遇到了同样的问题。 It sends the data to the server (PHP) but returns Error Undefined on error: handler. 它将数据发送到服务器(PHP)但在错误:handler处返回Error Undefined。

I have tried almost all other platforms and browsers (Linux - chrome, FF, Opera; Windows - IE, Edge; Android - Chrome, Opera) and everythings working as expected. 我已经尝试了几乎所有其他平台和浏览器(Linux - chrome,FF,Opera; Windows - IE,Edge; Android - Chrome,Opera),并且每个人都按预期工作。 Non of the browser on iOS is working. iOS上没有浏览器正在运行。 Good thing to mention is that the server is behind Amazon Cloudfront. 值得一提的是服务器落后于Amazon Cloudfront。 But I have let it pass all the Headers from the origin server and I can see correct Access-Control-Allow-Origin headers in the Chrome Developer Console. 但我让它从原始服务器传递所有Headers,我可以在Chrome Developer Console中看到正确的Access-Control-Allow-Origin标头。

I have also searched and tried all the suggestions with caching and relative URLs, JSON datatypes etc. with no luck. 我也搜索并尝试了缓存和相对URL,JSON数据类型等所有建议,没有运气。 I have also tried random parameter added to POST URL - no luck. 我也尝试将随机参数添加到POST URL - 没有运气。

Sometimes, let's say every third requests it is able to return right data. 有时,让我们说每三个请求它能够返回正确的数据。

My JS code: 我的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');
    }
  });
});

Here is the part of Server response code: 以下是服务器响应代码的一部分:

$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);

It should write Success but it alerts Rate problem. 它应该写成功但它会警告Rate问题。 Error: udnefined. 错误:udnefined。 textStatus: error, errorThrown: textStatus:error,errorThrown:

I believe you're testing on your local computer. 我相信你在本地计算机上进行测试。

When you test on your computer itself, "my/ajax/url" refers to the same domain. 当您在计算机上测试时,“my / ajax / url”指的是同一个域。 So let's say your test website is on http://localhost/mywebsite , AJAX will hit the full absolute URL which is " http://localhost/mywebsite/my/ajax/url " 因此,假设您的测试网站位于http://localhost/mywebsite ,AJAX将点击完整的绝对URL,即“ http:// localhost / mywebsite / my / ajax / url

On your iOS (I believe it's a different device, like iPhone/tablet/laptop/etc), when AJAX try to hit " http://localhost/mywebsite/my/ajax/url ", it's trying to find that URL in the same device (iphone/tablet/laptop/etc), NOT the computer where the website is hosted on. 在你的iOS上(我相信它是一个不同的设备,如iPhone /平板电脑/笔记本电脑/等),当AJAX尝试点击“ http:// localhost / mywebsite / my / ajax / url ”时,它正试图在相同的设备 (iPhone /平板电脑/笔记本电脑/等),而不是托管网站的计算机。

So: 所以:

  1. Make sure you're setting the correct URL. 确保您设置了正确的网址。 Put alert of full absolute URL (like " http://localhost/mywebsite/my/ajax/url ", and not like "my/ajax/url") BEFORE the AJAX. 在AJAX 之前提出完整绝对URL的警报(如“ http:// localhost / mywebsite / my / ajax / url ”,而不是像“my / ajax / url”)。
  2. In your PHP, put in first line this: echo "hi from server"; exit; 在你的PHP中,放入第一行: echo "hi from server"; exit; echo "hi from server"; exit; , and in your AJAX success function, put this: alert(data); ,并在你的AJAX成功函数中,把这个: alert(data);

Check if you're sending AJAX to correct URL (#1), and receiving from correct server (#2). 检查您是否将AJAX发送到正确的URL(#1),以及从正确的服务器(#2)接收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM