返回XML时使用JSONP [英] Using JSONP when returning XML

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

问题描述

我问过早先的问题,这肯定是有帮助的让我知道JSONP。但是,我发现我必须指定JSONP作为数据类型。现在,据我所知,这是返回的数据的返回类型,即XML。可以使用JSONP返回XML,还是仅限于以JSONP格式返回?谢谢!

I asked an earlier question which was definitely helpful and let me know about JSONP. However, I see that I have to specify JSONP as the datatype. Now, as far as I know, that is the return type of the data coming back, which would be XML. Can XML be returned using JSONP or am I limited to it coming back as JSONP format? Thanks!

推荐答案

由于它的工作原理,你只能使用JSONP(而不是XML)。 JSONP变成了这样:

You're limited to JSONP (and not XML) because of how it works. JSONP turns into this:

<script src="myPage?callback=myFunction" type="text/javscript">

因此,当您获取内容时,有效地执行此操作:

So when you take the content, it's effectively doing this:

<script type="text/javascript">
  myFunction({ data: value, data2: value2 });
</script>

回来的是实际运行的JavaScript,所以它不能是XML,你会得到所有各种语法错误,完全就像你这样做:

What comes back is actual running JavaScript, so it can't be XML, you'll get all sorts of syntax errors, exactly as you would doing this:

<script type="text/javascript">
  <elem>
    <data>value</data>
    <data2>value2</data2>
  </elem>
</script>

你可以想象,JavaScript解析器不会那么喜欢它,并且不会我不知道该怎么做。 jQuery可以在大多数情况下解析XML而不会有任何麻烦,但是如果你正在使用JSONP而且它是用于跨域请求......那么JSONP是你唯一的选择,除非你在你的网站上写了一个没有违反的代理页面同源策略规则,并将其用作通过提取XML的代理。

As you can imagine, the JavaScript parser isn't going to like that very much, and doesn't know what to do with it. jQuery can parse XML in most cases without any trouble, but if you're using JSONP and it's for cross-domain requests...well JSONP is your only option there, unless you wrote a proxy page on your site that didn't violate same-origin policy rules, and used it as a proxy to fetch the XML through.

这篇关于返回XML时使用JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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