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"
                        });
                    });
8
  • Share your code so we can see what you are doing.
    – thenetimp
    Feb 8, 2012 at 21:28
  • @thenetimp Code has been provided. Feb 8, 2012 at 21:40
  • Have you tried going to the URL manually or using Firefox's REST client to see if you get back the data you expect? Could be your source is not giving you the right data.
    – thenetimp
    Feb 8, 2012 at 21:47
  • Yes - the process works perfectly on desktop versions of Firefox, IE and Safari. Feb 8, 2012 at 21:51
  • 2
    We get the same issue. Everything is working, then you open a PDF into Safari's PDF viewer. When you return to the page, this error happens. Feb 9, 2012 at 19:30

3 Answers 3

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.

3
  • 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 8, 2012 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, 2012 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, 2012 at 21:59
3
+50

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.

4
  • 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. Feb 17, 2012 at 4:43
  • It does! Thank you for the answer. Do you know if there is a bug report open for this? Feb 19, 2012 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, 2012 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, 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, 2012 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

3
  • 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 2, 2012 at 5: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, 2012 at 6: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, 2012 at 6:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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