views:

2427

answers:

2

Hey,

solution (stupid!): The XML wasn't valid.. I forgot a /. Although I validated it earlier in w3schools, I added one tag and didn't close it properly.

original: I'm parsing an xml file with jQuery. In one node I store a JavaScript function with CDATA:

<action is="javascript"><![CDATA[
alert("Holy Crap!");
]]>
</action>

now I crawled the web for solutions and found out that I need to specify "text/xml" as content-type for $.get() for the CDATA to be processed properly. Making my $.get as follows:

$(document).ready(function() {
$.get("wired.xml", {}, xmlToToolbar, 'text/xml');
});

this still doesn't work. I'm using FF. alerting the xml content will show blank where the CDATA content should have been. If this helps, here is the line I use to retrieve that same node:

$(this).find("action").text();

edit with more info: I used text/xml because I read that Firefox treat XML like that and IE with 'xml'.

also, this works:

$(document).ready(function() {
$.post("wired.xml", {}, function() {
     alert("yeah!");
});
});

but this doesn't:

$(document).ready(function() {
$.post("wired.xml", {}, function() {
 alert("yeah!");
}, 'xml');
});

and this work:

$(document).ready(function() {
$.post("wired.xml", {}, function() {
 alert("yeah!");
}, 'text/xml');
});

but treats the xml as html.

Thank you.

+1  A: 

Use "xml" as the last parameter instead of "text/xml". Possible data types for .get include: "xml", "html", "script", "json", "jsonp", or "text".

$.get("wired.xml", {}, function(data, textStatus) {
    //Get the contents of the action tag
    var actionContents = $(data).find('action').text();

    //Popup the alert
    eval(actionContents);
}, 'xml');
atogle
Does not work. If i write 'xml' it doesn't work at all yet 'text/xml' or even 'blabla/xml' produces the xml as html.
Radagaisus
I am certain that 'xml' is the correct value to have $.get return XML content. http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktypePerhaps I misunderstood your intent. I understand that you want to read the contents of the XML file using AJAX and evaluate the JavaScript inside the action tag. Is that correct? If so, I successfully ran the code above to do just that using jQuery 1.3.2.
atogle
i've read the documentation, that's way this is so weird. I'm going to take a working example and push my code in it and see if that works.
Radagaisus
A: 

I have exactly the same scenario as you, Radagaisus!

I have a valid XML with two nodes, one has a number as the content - that one is read fine by JQuery's text(). The other one is with CDATA, and that is not read.

I've tried to add that content type "xml", and as soon as I do it, the callback function is not even run anymore. One difference for me though — "text/xml" acts as an invalid type value apparently, and I get the same result as if no type was set.

Radagaisus, have you come to any solution?

=========================

OK, I'll answer my own question. :)

Apart from a misspelt function name that gave me a fatal error (so some of my observations might have been affected by it), the solution was to output a content type header: <? header ('Content-Type: text/xml; charset=utf-8'); ?> as by default it was given as text/html (AJAX was calling a .php file with XML output).

Janis