10

Using the latest version of jQuery 1.6 on iOS 5 safari from an iPad, I'm noticing that all my ajax calls are failing. These same ajax calls work as expected on all other browsers I've tried, and I'm pretty sure they were also working on iOS 4's version of Safari (although I could be wrong). Has anyone else experienced this behavior as well? If so, is there a fix or workaround? Below is a quick example of a simple jQuery AJAX call that is returning an error in iOS 5's Safari. Thanks in advance for any insight!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
  </head>
  <body>
    <a id="my-link" href="javascript:;">Click Me!</a>
    <script type="text/javascript">
      jQuery(document).ready(function() {
        jQuery("#my-link").bind("click", function() {
          jQuery.get("test.php", function(data) {
            alert(data);
          });
        });
      });
    </script>
  </body>
</html>
7

6 Answers 6

15

I had a similar issue just now. I had

 $.ajax({
        type: 'post',
        url: 'http://IP../ws',
        data: {1:1},
        dataType: 'json',
        success: function(response) {
            if (lng.Core.toType(callback) === 'function') {
               // setTimeout(callback, 100, response);
                callback(response);
            }
        },
        error: function(xhr, type) {
            console.log('error')
            if (error) {
                setTimeout(error, 100, result);
            }
        }
    });

changed url: 'http://IP../ws', to url: 'ws',

I'm not a jQuery user at all but have to use it for a project so not sure if this is help to you or not but worked for me.

4
  • I agree with Daithi. The only way I could get my AJAX call to run was to use a RELATIVE path rather than an ABSOLUTE path.
    – Doug
    Aug 10, 2012 at 21:24
  • Yes. This was it. DEAD on. Should be flagged as the right answer. Mar 11, 2013 at 5:49
  • I can't believe this fixed it, I swear the same function was working for over a month, then today it stopped running the callback function - perhaps it is the result of a false-positive security check, as it was still working in Chrome and all my other devices. Anyway, I'm happy, thank you!
    – tylerl
    Apr 24, 2013 at 17:27
  • Worked for me, this should be the flagged as the right answer.
    – Brian
    Nov 26, 2016 at 19:21
1

Restart Safari - leave it and kill it from the running tasks.

From other things I have read it is related to security contexts and prevention of cross site scripting attacks, and Safari not getting things quite right when it was previously running on a different network and is now on an new network without it having been stopped between changing networks.

Ran into it myself today, w/ plain HTML/JavaScript/PHP XMLHttpRequest request.

1

I had face one issue that jQuery ajax call fail in IPad in Safari browser. Error is you have no permission to access the page / directory. I fix the issue by changing the Ajax async property to true.

 $.ajax({
        type: 'GET',
        url: "".
        async: true,
        cache: true,
        crossDomain: false,
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        error: function (jqXHR, err) {
            console.log(jqXHR.responseText);
            alert("Error" + jqXHR.responseText);
        },
        success: function (data, status) {

            });
0

There is a bug in Safari Mobile that suddenly and unexpectedly gives troubles with AJAX calls if there is any file serving going on. Safari can starts sending "OPTIONS" http messages rather than POST after it has been served a download with a Content-Disposition: attachment; header. You can look to see if this is happening by using Fiddler between Safari Mobile and the server to see what HTTP messages Safari is sending.

That "condition" in Safari Mobile is reset when restarted.

It is reviewed well here Stackoverflow: JQuery Ajax stopped working with IOS 5.0.1.

0

I've had to make it so that it re-try up to 20 times on error to make it work. Code example:

function my_original_function(form)
{
  my_original_function_ajax(form, 1);
}

function my_original_function_ajax(form, attempts)
{
  console.log('Attempts #'+(attempts));

  jQuery.ajax({
    type: 'POST',
    url: form.action,
    processData: false,
    data: $(form).serialize(),
    cache: false,
    success: function(html){
       console.log('success!!');
    },
    error: function (xhr, status, error) {
      // make up to 20 attempts if error
      if (attempts <= 20) {
        my_original_function_ajax(form, attempts + 1);
      }
    }
  });
}
0

I had the same issue but I ended up discovering something different totally.

I had something like:

function load_one(var1 = null, var2 = null) { ... }
function load_two() { ... }

And after that I had

$(window).load(function() {
   load_one(var_x, var_y);
   load_two();
});

Everything worked fine in Chrome, Firefox, Safari for OSX, Safari for iPhone, Chrome for iPhone, but on Edge and Safari for iPad nothing worked. So I opened it on Edge and inside the developer tools it was showing an error on the line where the load_one function was defined.

I wasn't sure what it was but the error said ) expected so I decided to remove the default values for the function parameters and everything worked all of a sudden. I am not sure if javascript has issues with default parameter values, but apparently some browsers have issues with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.