How to use xml response as XMLObject outside of ajax callback function

Posted by Anthony on Stack Overflow See other posts from Stack Overflow or by Anthony
Published on 2010-05-01T00:43:57Z Indexed on 2010/05/01 0:47 UTC
Read the original article Hit count: 293

Hopefully I've just made a dumb oversight, but I can't figure out why the following doesn't work:

$(function() {
var xml;
    $.get(
        "somexml.xml",
        function(data){
            xml = data;
        },
        "xml");
    alert(xml);
});

If I put the alert inside of the callback function, I get back object XMLdocument but if I place it outside of the ajax call, I get undefined.

Since my goal is to have a new DOM to parse, I don't want the entire handling of the XMLdocument to be within the callback function.

I've tried defining the variable outside of the entire onready function, inside at the top (like above) and inside the callback function, all with no luck.

According to Specifying the Data Type for Ajax Requests in the jquery documentation, this should be possible.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX