简体   繁体   English

使用jQuery获取跨站点XML响应

[英]Getting Cross-site XML response with jquery

I'm trying to get some xml information from a service that is not mine. 我正在尝试从不是我的服务中获取一些xml信息。 Basically, the user will authenticate on the other service and my script should go get the information using it authentication cookie via cross-site. 基本上,用户将在其他服务上进行身份验证,并且我的脚本应该通过跨站点使用身份验证cookie来获取信息。

I'm using jquery to do that, and I can see that the response I want to process is returned by the service (via firebug), the thing is that I'm using jsonp to do the call, so jquery returns a parsing error. 我正在使用jquery来执行此操作,并且可以看到要处理的响应由服务返回(通过firebug),问题是我正在使用jsonp进行调用,因此jquery返回了解析错误。

I've tried all the solutions that I encountered to do this kind of operation, like YQL and proxy server. 我已经尝试过遇到的所有解决方案来执行这种操作,例如YQL和代理服务器。

My frustration in here is that I'm seeing the response I want but jquery just don't give me the raw information. 我在这里的沮丧之处在于,我看到了想要的响应,但是jquery却没有给我原始信息。

I know that I'm calling a function that expects json response, but isn't any workaround or other way to this? 我知道我正在调用一个期望json响应的函数,但是这没有任何解决方法或其他方法吗? like xmlp xD. 像xmlp xD。

$.ajax(
        {
            url: "serviceurl",

            dataType: 'jsonp', //I've tried 'jsonp xml'


            crossDomain: true,
            xhrFields: {
                withCredentials: true
            },
            success: function()
            {
                alert('Load was performed.');
            },

            error: function(jqxhr,error) 
            { 
                alert('Failed!'); 
            },

        });

EDIT: Here is the output from the server 编辑:这是服务器的输出

<User>
 DVD_PT
</User>
<Apps>
 <App>
   <name>Last.fm Scrobbler</name>
 </App>
</Apps>

Thanks 谢谢

It simply isn't possible to request XML from a cross-domain source unless the other domain supports CORS. 除非另一个域支持CORS,否则根本不可能从跨域源请求XML。

You must use some sort of proxy such as YQL, as you have already mentioned. 正如您已经提到的,您必须使用某种代理,例如YQL。

The response you are seeing when you set the datatype to JSONP is inside of a <script> tag. 将数据类型设置为JSONP时看到的响应位于<script>标记内。 For example, you are getting this: 例如,您得到以下信息:

<script src="http://example.com/note.xml"></script>

where note.xml is: 其中note.xml是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

which will of course throw an error because it is not valid javascript. 这当然会引发错误,因为它不是有效的javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM