9

In attemping to get Safari on iOS 5 to load an AJAX request via jQuery, the debug console loads with the error of

Javascript: Error on Line 1
XMLHttpRequest cannot load http://<MYSERVERADDRESS>/WebMethods.asmx/Method. Cannot make any requests from null

I've tried to Google the answer, as well as search SO, but I can't find a resolution.

This issue is only present on iOS.

Any help would be greatly appreciated.

Edit

Below is the code as requested.

jQuery(function ($) {
                        $.ajax({
                            url: "/Peak.asmx/IsValidParticipant",
                            data: { LanID: $("#LoginPageUsername").val(), Password: $("#loginPagePassword").val() },
                            type: "POST",
                            success: function (data) {
                                if (data.response.result == "Success") {
                                    window.location = "/RSVP.aspx";
                                } else if (data.response.result == "Failure") {
                                    $(".errorsSummaryBox").show().find("li").hide();
                                    if (data.response.data.Reason == "Credentials") {
                                        $("#PortalAuthError").slideDown();
                                    } else if (data.response.data.Reason == "Participant") {
                                        window.location = "/NoInvite.aspx";
                                    }
                                }
                            },
                            dataType: "json"
                        });
                    });
Tim Ferrell
  • 1,348
  • 3
  • 17
  • 40

3 Answers3

9

I had the same problem, but I found the solution!

You have to restart the Safari process. Not only exit from the main window,but:

  • exit from Safari
  • double click on the exit button to show the active process
  • hold on Safari
  • click on minus
  • restart safari

strange but true.

Redax
  • 9,231
  • 6
  • 31
  • 39
  • This is the quick and dirty fix. The underlying cause of the problem for us was loading a PDF in the same tab as well. – Jon Mar 08 '12 at 19:23
  • 3
    Quick, but not so dirty.I had the same problem, but I am not using any PDF: different cause, same error, same solution. I think is a kind of bug of Safari, so reopen is not dirty. – Redax Mar 13 '12 at 11:18
  • 1
    This fixed it for me - only I was requesting a basic HTML file. When I restart Safari, it works fine. Not sure what the problem is... as Chrome on iPad works fine. – mdance Sep 13 '12 at 21:59
3

To TFerrell:

Does RSVP.aspx response contains a PDF? If it is then try using my suggestion to Parched below.

To Parched:

The PDF you are trying to open will break the default PDF viewer of the new Safari mobile 5. The quick solution is to popup a new window for the PDF. This will break the new popup page but not the parent page. Another solution is to generate (if you are) the PDF in a well-formed PDF format.

Vitruvius
  • 96
  • 1
  • 1
    Yeah, we ended up opening the PDF into a new tab. Apart from avoiding this problem, it actually works better anyways. I don't think this (PDF's) is the only situation where it happens though. It's just one. – Craig Celeste Feb 17 '12 at 04:43
  • It does! Thank you for the answer. Do you know if there is a bug report open for this? – Tim Ferrell Feb 19 '12 at 17:54
  • As of now, I am not aware of one that is specifically for this quirk. Yes, it is something like a quirk and not a bug - IMO. – Vitruvius Feb 22 '12 at 15:07
  • 2
    The same thing happens if you redirect to anything with a `Content-Disposition: attachment` HTTP-header, even images. Found something that seems to explain the cause of it [here](http://support.apple.com/kb/HT4999), search for 'Content-Disposition'. So iOS opens an isolated security origin for download files. My guess is, that when going back to the previous page, this page is executed in that new security context. Probably that context is local and doesn't allow remote request, and thus the XHR object is null. Looks like a bug to me... – meyertee Jun 28 '12 at 17:24
1

Please remove the answer if this is not helping/correct ... I got not enough reputation to add a comment and this is only a guess, but I had a very similar issue today:

I was trying to get a jquerymobile thing to run in which I had an ajax request. To debug I switched to desktop chrome and it would always give me an error in the console: "XMLHttpRequest cannot load [url]. Origin null is not allowed by Access-Control-Allow-Origin."

The reason was because I was loading the ajax from file:///some.html of some different domain. Therefore this was a cross-site issue ..

I googled and found file:// sites (as for example if you use a phonegap made site on a mobile) do have the origin null and that issue seems to be difficult.

Anyway, I believe this might be a lead for you. You might need to configure your server to allow crossdomain-requests.

See this question for details XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

See this guide for more help http://usejquery.com/posts/the-jquery-cross-domain-ajax-guide

Personally I didn't try to solve this in depth as it works on my android emulator ... (I tried to set the "crossDomain" and "xhrFields" property on jquery-ajax to no avail)

cheers

Community
  • 1
  • 1
Joehannes
  • 301
  • 2
  • 9
  • Yep, I am using Phonegap and I don't have an answer to the file:// urls too. Did you manage to solve the issue? please help if you did – thandasoru Apr 02 '12 at 05:50
  • I'm sorry, I didn't really investigate further - though I doubt it is currently possible to circumvent this at all (at least in chrome) (unless you find the chrome startup-script and add some flag) .. though I personally did have the same probs in firefox ... However, what I did was the following: -------------------------------------- * Use JS-MVC (Framwork) * Use the fixtures feature – Joehannes Apr 11 '12 at 06:14
  • What this is in general terms? -------------------------------------- I just added an error handler to my ajax calls. If the error would be triggered it wouldn't try to download the files, but I included them in my app and they would get updated (ideally) each time it works (actually it always works from the phone ...) - and use the (updated/downloaded) local files would be used should the ajax call fail ... ***sorry*** - no real workaround, very much tailored to my needs here, cheers – Joehannes Apr 11 '12 at 06:19