0

Just trying to load a simple XML file in my Marmalade web app (based on PhoneGap) with the following jQuery code:

$.ajax({
    type: "GET",
    url: "books.xml",
    dataType: "xml",
    success: function(xml) {
        alert("SUCCESS");
    },
    error: function(result) {     
            alert("FAILED : " + result.status + ' ' + result.statusText);  
    }
});

I'm getting a 404 error, however. The file is in the same directory as the HTML file the JavaScript above is contained in. I've also tried adding "file:///" to the beginning of the URL, but to no avail.

After searching for a couple of hours, I've found no solutions for the problem. Any ideas?

Your help is appreciated! -SL

Lou Alicegary
  • 856
  • 7
  • 6

1 Answers1

0

First thing I would do is take jQuery out of the equation and just try a plain vanilla AJAX call:

var request = new XMLHttpRequest();
request.open("GET", "books.xml", true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        if (request.status == 200 || request.status == 0) {
            alert("SUCCESS");
            var books = request.responseXML;
        }
    }
}
request.send();

If books.xml is in the same directory as your HTML file that should just work.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Simon -- Thanks for the answer. In my post I failed to mention that I had tried such a plain-vanilla call previously before attempting the jQuery version. I tried your solution, and the "success" alert fires, but then the execution hangs on the request.send() function. If I place an alert after that code, it never fires. Thus, it appears that the problem is with the sending of the AJAX request. Any insights into why? – Lou Alicegary Aug 09 '12 at 23:18
  • That doesn't make sense. The request is async so if you are seeing the success alert then request.send has already executed. Any code after request.send should be executed immediately and you'll see the alert dialog when the request has succeeded. – Simon MacDonald Aug 10 '12 at 16:02
  • 1
    Despite "success" firing, if I try to run any sort of procedure on the "books" XML response (getting nodes, etc.) it always comes back as undefined. Strange stuff. – Lou Alicegary Aug 10 '12 at 20:42
  • That is very strange. What version of Android are you using? – Simon MacDonald Aug 13 '12 at 15:38
  • It's an iOS app running in the simulator. I've since scrapped using Marmalade (which has horrible support) and have ported my code over to XCode / Cordova. I'll update you if the problem goes away or if it persists. Thanks again for the feedback...marking this as answered, just because I've given up on it! – Lou Alicegary Aug 15 '12 at 08:24