Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from jQuery XML Object to String throws Security Error

I have a XML Object generated by jQuery from the response of a REST Webservice:

$.ajax({
    type: "GET",
    url: "http://localhost:9299/foo",
    success:function(xml) {
        xmlDoc = $.parseXML(xml);
        $xml = $(xmlDoc);
        // The rest of the code manipulates the structure of the XML
    }
});

Now I need to output the altered XML Object as a String. I've already found this solution for Firefox and other browsers:

out = new XMLSerializer().serializeToString($xml);

But all I get here is the following error message:

[Exception... "Security error"  code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)"  location: "http://localhost/bar"]

All files I need are on localhost (including the webservice which provides me the XML and the jQuery library)

Any ideas would be highly appreciated

Edit:

I've simplified the problem and tried the following code:

$xml = $('<?xml version="1.0"?><root><element>bla</element><element>blubb</element></root>');
$xml.find("element").each(function() {
    alert($(this).text());
});
out = new XMLSerializer().serializeToString($xml);

Even without any webservice call the problem remains the same. (The alert outputs the content correctly)

Edit 2:

Thanks to the comment by Kevin B, I've got a working solution:

$.ajax({
    type: "GET",
    url: "http://localhost:9299/foo",
    dataType: 'xml',
    success:function(xml) {
        var $xml = $(xml);
        // The rest of the code manipulates the structure of the XML
    }
});

The last line doesn't change:

out = new XMLSerializer().serializeToString($xml);
like image 461
Velarion Avatar asked Jan 26 '26 13:01

Velarion


1 Answers

First off, I can't confirm/deny based on your code whether or not this is a cross-domain request. Cross-domain is when the port number, domain, or protocol of the external file is different from the one requesting the external file.

If it is indeed a cross-domain request, you need to implement CORS or a server-side proxy to request it for you.

Secondly, you don't need to use $.parseXML(). Try this:

$.ajax({
    type: "GET",
    url: "/foo",
    dataType: "xml",
    success:function(xml) {
        var $xml = $(xml);
        // The rest of the code manipulates the structure of the XML
    }
});

The XML also must be valid for it to work in all browsers.

Edit: So, it's not a cross-domain issue, and it is not a jquery issue. Here's some more debugging: http://jsfiddle.net/RKpua/ I used a very simple xml document there, can you replace the simple xml document with your xml?

like image 67
Kevin B Avatar answered Jan 28 '26 02:01

Kevin B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!