如何处理使用jQuery从jsonp返回的xml? [英] How to process xml returned from jsonp using jquery?

查看:287
本文介绍了如何处理使用jQuery从jsonp返回的xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下jquery返回位于相同子域的xml:

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

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

现在,每当我在Firebug中运行此命令时,我都会在Firebug中收到一个JS错误,说:Missing;声明前.返回的数据如下所示:

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>

返回了我想要的数据,但是我不确定如何使用它.我需要访问 somedata ,但是无法访问. Firebug甚至没有停止该功能.我如何正确进行?

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?

推荐答案

它看起来好像您期望返回XML,但您正在调用需要JSON的函数. XML和JSON是两种不同的数据编码方式.

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.

如果要以字符串形式获取XML,则可以使用jQuery的get函数.这将需要您自己解析该字符串才能提取...somedata....

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....

但是,如果您想使用jQuery处理XML响应的内容,那么最好的选择是使用ajax函数:

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();
    }
});

这篇关于如何处理使用jQuery从jsonp返回的xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆