Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Gabriel D. Celery
13,810 PointsAjax - issue with returned XML
Let's say I have a very simple XML file thats saved as "sample.xml".
<address>
<name>Joe Tester</name>
<street>Baker street 5</street>
</address>
Now if I try to do this:
$.ajax({
method: "GET",
url: "sample.xml",
success: function (data){
var $xmlData = data;
console.log($xmlData.find("name").text());
}
});
I get an error, that $xmlData.find is not a function. But if I do this:
$.ajax({
method: "GET",
url: "sample.xml",
success: function (data){
var $xmlData = $(data); // Here is the change
console.log($xmlData.find("name").text());
}
});
The code runs as expected. My question is why is this happening? The reason why its confusing is because I don't have to do this if I am requesting JSON.