How to handle xml returned by jsonp using jquery?

I use the following jquery to return xml, which is in the same subdomain:

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

Now when I run this in firebug, I get a js error in firebug saying: Missing; before the expression. The returned data is as follows:

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

The returned data is returned, but I'm not sure how to use it. I need to access somedata strong>, but I can’t. Firebug doesn't even stop in function. How to continue the work?

+4
source share
1 answer

It seems that you are expecting XML to return, but you are calling a function that expects JSON. XML and JSON are two different ways of encoding data.

If you want to get XML as a string, you can use the jQuery get function. This will require you to parse the string yourself to extract ...somedata...

But if you want to process the contents of the XML response using jQuery, then it is best to use the ajax function:

 $.ajax({ url: myurl, dataType: 'xml', success: function(data) { debugger; alert(data); // untested: var theValue = $('string', data).text(); } }); 
0
source

Source: https://habr.com/ru/post/1312366/


All Articles