4

The following piece of logic used to work with both jquery 1.4.4 and 1.7.1 on all mobile devices that we're supporting:

$.ajax({
  url: 'http://www.example.com/someurl',
  type: 'GET',
  dataType: 'text',
  timeout: 60000,
  success: function(data) {
    alert(data);
  },
  error: function(jqXHR) {
    alert(jqXHR.state());
  }
});

But with iOS 5.0.1, the above enters the error function alerting rejected without any actual HTTP requests. It seems that exactly one AJAX request will work before I need to restart my iPhone. Is this a known jquery / iOS 5.0 problem? How can I debug it? Is there any workaround? I don't know where to start looking.

NOTE: I've noticed on the server side, that instead of GET requests, OPTIONS requests are issued. This seems to be a related issue:

http://spin.atomicobject.com/2012/01/20/mobile-safari-on-ios-5-1-unexpectedly-making-cross-origin-resource-sharing-requests/

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • Have you looked at the traffic between the phone and the server using something like Charles? – tkone Feb 09 '12 at 12:27
  • @tkone: Hmm, yes I could find some traffic being blocked by our web entry server. It seems that the iPhone issues an `HTTP OPTIONS` request, which we reject... But why...? – Lukas Eder Feb 09 '12 at 13:25
  • that's very odd. Do you have a sample page I can point my phone at? I have an iOS4 device as well I want to see if it happens on as we are about redo our mobile site to rely extensively on ajax... – tkone Feb 09 '12 at 14:00
  • @tkone: Unfortunately, no. I can't make this publicly available... :-/ But iOS4 is still fine – Lukas Eder Feb 09 '12 at 14:10
  • @tkone: N.B. I'm starting to *feel* that iOS 5 doesn't send cookies with `XMLHttpRequest` :-/ – Lukas Eder Feb 09 '12 at 14:43
  • @tkone: I found the issue. This might be interesting to you, too. Beware of PDF downloads on iOS 5!! – Lukas Eder Feb 09 '12 at 15:57

3 Answers3

5

The link that I provided in the question is actually pointing to the solution. Some of my ajax requests are used to fetch URL's of PDFs which are streamed using

Content-Disposition: attachment; filename="somename.pdf"

Apparently, that causes major issues in iOS 5.0's Safari, breaking the XMLHttpRequest object (it is not related with jquery). Crazy. Here's the link again:

http://spin.atomicobject.com/2012/01/20/mobile-safari-on-ios-5-1-unexpectedly-making-cross-origin-resource-sharing-requests/

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • So it was the content-disposition header that is causing it to break? You should file a bug at radar.apple.com about this -- I didn't find one when searching in there. Thanks for the tip! – tkone Feb 09 '12 at 16:01
  • @tkone: Good idea! Filed as issue `10835750` – Lukas Eder Feb 09 '12 at 17:06
1

Thank you for the investigation. I am having the same problem. After receiving a file as 'attachment' Mobile Safari sends OPTIONS request when calling:

$.ajax('/url')

BUT, if I run the following code from the same file, it generates a valid GET request:

http = new XMLHttpRequest()     
http.open("GET", "/url")
http.send()

Why jQuery is not working but XMLHttpRequest works here?

Evgenii
  • 36,389
  • 27
  • 134
  • 170
-1

The reason for the error is the same origin policy. It only allows you to do XMLHTTPRequests to your own domain. See if you can use JSON instead.

References and recommended resources;
https://stackoverflow.com/a/1109261/896341
http://api.jquery.com/jQuery.getJSON/
http://www.w3.org/Security/wiki/Same_Origin_Policy
https://developer.mozilla.org/En/Same_origin_policy_for_JavaScript

Community
  • 1
  • 1
Stefan
  • 5,644
  • 4
  • 24
  • 31
  • I'm sending the request to the same domain. All requests are sent to the real-life equivalent of `http://www.example.com`. `JSON` is not an option in our setup... – Lukas Eder Feb 09 '12 at 14:14