4

I use the following jquery to return an xml that resides on the same subdomain:

$.getJSON(myurl, function(data) 
{ 
  debugger; 
  alert(data); 
});

Now whenever I run this in firebug, I get a js error in firebug saying: Missing ; before statement. The data returned looks like this:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="somenamespace">...somedata...</string>

The data I want is returned, but I am not sure how to use it. I need to access somedata, however I am not able to. Firebug doesnt even stop in the function. How do I proceed properly?

vikasde
  • 5,681
  • 10
  • 45
  • 62

1 Answers1

0

It appears as if you're expecting XML to be returned but you're calling the function which expects JSON. XML and JSON are two different ways of encoding data.

If you want to get the XML as a string then you can use jQuery's get function. This would require that you parse the string yourself in order to extract ...somedata....

But if you would like to process the content of the XML response with jQuery then your best bet is to use the ajax function:

$.ajax({
    url: myurl,
    dataType: 'xml',
    success: function(data) {
        debugger;
        alert(data);
        // untested:
        var theValue = $('string', data).text();
    }
});
Ken Browning
  • 28,693
  • 6
  • 56
  • 68
  • 1
    Thanks, Does that work when the url is on a different domain? – vikasde Jun 10 '10 at 13:29
  • I get a 403 when using the xml datatype. – vikasde Jun 10 '10 at 13:33
  • 1
    http://api.jquery.com/jQuery.ajax/#dataType For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml. – jhanifen May 09 '11 at 20:49
  • 6
    @jhanifen Theoretically the `'jsonp text xml'` should work, but for some reason it does not and returns `Uncaught SyntaxError: Unexpected token < ` – hitautodestruct Dec 20 '12 at 13:19
  • @hitautodestruct agreed. – heyomi Jul 01 '15 at 16:51