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>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
ajporterfield
  • 1,009
  • 2
  • 10
  • 23
  • 1
    Safari's error console is returning the following..."XMLHttpRequest cannot load http://www..com/test.php. Cannot make any requests from null" – ajporterfield Oct 30 '11 at 03:22
  • I should also mention that the AJAX call is making a GET request to the same domain. – ajporterfield Oct 30 '11 at 03:25
  • http://bugs.jquery.com/ticket/10529 – mu is too short Oct 30 '11 at 06:42
  • This works okay for me on my iPad (iOS5): http://jsfiddle.net/ambiguous/zWC3R/ so maybe see if you can change that to match what you're seeing and then kick the jQuery people in the butt with a test case. – mu is too short Oct 30 '11 at 06:52
  • i have the same problem that occurs only on ios5(ipad). apparently it is a bug : http://bugs.jquery.com/ticket/10529 , and i'm still waiting for a solution – gion_13 Nov 16 '11 at 14:23
  • I was getting this problem, but it went away after rebooting the iPad... it may be an ios/safari bug instead of a jquery bug. – Jay K Dec 02 '11 at 19:29
  • Just not to duplicate post. Probably this could help: http://stackoverflow.com/a/18514269/1872856 – Alex Aug 29 '13 at 15:05

6 Answers6

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.

Daithi44
  • 151
  • 2
  • 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 '12 at 21:24
  • Yes. This was it. DEAD on. Should be flagged as the right answer. – J Cole Morrison Mar 11 '13 at 05: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 '13 at 17:27
  • Worked for me, this should be the flagged as the right answer. – Brian Nov 26 '16 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.

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
prniii
  • 31
  • 2
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) {

            });
Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79
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.

Community
  • 1
  • 1
Mark Kasson
  • 1,600
  • 1
  • 15
  • 28
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);
      }
    }
  });
}
Online Sid
  • 116
  • 4
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.

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41