I'm trying to assign the contents of an xml file to a var, like so:
var testing = $.load('xx.xml');
$('#display').text(testing);
but it's not working. I tried the ".load" function as suggested in:
How to assign file contents into a Javascript var
and I had a look at the page they suggest form the jquery website, but I can't find something specific to assigning the contents of the .xml file to the var as a string.
I appreciate this is probably very obvious & I am arguably being lazy, but I have been trying random things for a while & cannot figure this out.
Thanks
EDIT! I was loading up the contents inside the load function, i didn't mean this, it's now edited.
I'm trying to assign the contents of an xml file to a var, like so:
var testing = $.load('xx.xml');
$('#display').text(testing);
but it's not working. I tried the ".load" function as suggested in:
How to assign file contents into a Javascript var
and I had a look at the page they suggest form the jquery website, but I can't find something specific to assigning the contents of the .xml file to the var as a string.
I appreciate this is probably very obvious & I am arguably being lazy, but I have been trying random things for a while & cannot figure this out.
Thanks
EDIT! I was loading up the contents inside the load function, i didn't mean this, it's now edited.
Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Oct 14, 2011 at 9:28 Jim BoJim Bo 6573 gold badges9 silver badges16 bronze badges 3- As Alex R. said, text is only for chars, not HTML. Also, why would you load the var up inside the call? – Marco Johannesen Commented Oct 14, 2011 at 9:33
- Whoops, that was a school boy error, I pasted fronm an older version, code changed. – Jim Bo Commented Oct 14, 2011 at 9:34
- @MarcoJohannesen, it actually won't work, so I had to retract my answer. – Alex R. Commented Oct 14, 2011 at 9:35
3 Answers
Reset to default 4Firstly, $.load
isn't a defined function in the latest jQuery source, nor is it documented on the jQuery site.
Secondly, assuming you haven't modified jQuery's global AJAX settings, jQuery.fn.load
and other request functions will be asynchronous, so you can't just assign the result to a variable because the function returns before the request has pleted. You need to use callback handlers.
Try using $.ajax
with a callback function instead:
var testing;
$.ajax('xx.xml', {
dataType: 'text',
success: function (data) {
testing = data;
$('#display').text(testing);
}
});
Since you want the data as text and the file appears to be XML, we're using dataType
to tell jQuery to return the data as a string.
There is no $.load
function. What you probably want is jQuery.get
:
var xml;
$.get("xx.xml", function(data) {
xml = data;
});
As the file is retreived asynchronously, you need to assign the result to the variable inside the callback, which is executed when the request returns successfully. Note however that if you try and run code that depends on xml
after the .get
call, xml
is going to be undefined because the callback won't have run yet. For example:
var xml;
$.get("xx.xml", function(data) {
xml = data;
//Do stuff with data here
});
console.log(xml); //Most likely undefined because asynchronous call has not pleted
If you are trying to insert the results into a DOM element, then you can use the .load
method:
$("#someElem").load("xx.xml");
if you are trying to get an xml from your server using ajax, you may try something like this -
function getXml()
{
var contents;
$.ajax({ url :'/website/method', type: 'GET', dataType :'xml', async : false,
cache : true, success : function(myXml){
contents = myXml;
}
});
return contents;
}