需要帮助使用JavaScript获取跨域XML [英] Need Help With Getting Cross Domain XML With JavaScript

查看:91
本文介绍了需要帮助使用JavaScript获取跨域XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在建立一个网路应用程式,提供音乐资讯(例如关于艺术家,专辑,歌曲等的资讯),以及使用MusicBrainz API的资讯来源。



现在,我试图从API调用加载数据并使用jQuery处理它。这是我使用的代码:

 代码:
queryString =http://musicbrainz.org/ ws / 1 / artist /?type = xml& name =+ qry +& limit = 10;
$ .ajax({url:queryString,dataType:($ .browser.msie)?text:xml,success:function(data){
alert(success);
var xml;
if(typeof data ==string){
xml = new ActiveXObject(Microsoft.XMLDOM);
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
};
...

使用'queryString'作为请求的URL字符串,然后我将继续从'xml'对象读取数据。 p>

然而,这是出现问题的地方。代码在我的电脑上本地运行时完美无缺,但是当我把所有东西上传到我的网络服务器并尝试运行我做了一些阅读,并发现AJAX调用不能在不同的域,由于安全问题。



因此,我已经阅读了许多解决方案,但几乎都需要任何PHP(我完全不知道)或抓取JSON格式的数据(显然不受相同的安全限制)。但是,我的主要问题是MusicBrainz API不返回JSON格式的数据(实际上,它返回的唯一格式是XML)。



所以在任何情况下,我基本上只是想知道是否有人可以给我一些帮助或指针,如果和如何我可以抓住远程XML文件只使用JS / jQuery 。或者,指向另一个方法,可以完成一个完整的PHP noob像我自己。



感谢您的帮助!

解决方案

服务器端将您的请求代理到该其他服务器。以下网址:

  /proxy?url=http%3A//musicbrainz.org/ws/1/artist/ %3Ftype%3Dxml%26name%3Dexample%26limit%3D10 

如果PHP在您的服务器上可用, 这里是一个<

PHP脚本将检索指定的URL:

 <?php readfile 'url'])?> 

请注意,您将无法POST任何数据,或指定Content- Type 。这是非常基本需求所需的最基本的代理。






我知道JSON现在不是一个选项, ,



JSON是Javascript,可以使用< script> 标记,而不是XMLHttpRequest。由于< script> 标记对跨网域请求没有相同的限制,因此可以通过这种方式检索JSON内容。



此技术称为 JSONP < a>,并在 getJSON 函数中的jQuery中实现。


Alright, so I'm building a web app that provides music information (i.e. info on artists, albums, songs, etc.) and for the info source I'm using the MusicBrainz API.

Now, I'm trying to load the data from an API call and process it, with jQuery. This is the code I'm using:

Code:
queryString="http://musicbrainz.org/ws/1/artist/?type=xml&name="+qry+"&limit=10"; 
$.ajax({url: queryString, dataType: ($.browser.msie) ? "text" : "xml", success: function(data){ 
      alert("success"); 
      var xml; 
      if (typeof data == "string") { 
             xml = new ActiveXObject("Microsoft.XMLDOM"); 
             xml.async = false; 
             xml.loadXML(data); 
        } else { 
             xml = data; 
        }; 
...

With 'queryString' being the URL string for the request, and then I'd proceed to read the data out of the 'xml' object. Fairly simple.

However, this is where problems arise. The code works flawlessly when running locally on my computer, but does not work at all when I upload everything to my web server and try to run it there. I did some reading and have discovered that AJAX calls can't be made across different domains, due to security issues.

So I've read through numerous solutions, but almost all require either something with PHP (which I have absolutely NO knowledge of) or grabbing the data in JSON format (which apparently isn't subject to the same security restrictions). However, my main problem is that the MusicBrainz API does not return data in JSON format (in fact the only format it returns is XML).

So in any event, I was basically just wondering if anyone could give me some help or pointers on if and how I could grab that remote XML file using only JS/jQuery. Or, point me toward another method that could be accomplished by a complete PHP noob like myself.

Thanks for any help!

解决方案

You require something on your server side to proxy your request to that other server. A URL that looks like:

/proxy?url=http%3A//musicbrainz.org/ws/1/artist/%3Ftype%3Dxml%26name%3Dexample%26limit%3D10

If PHP is available on your server, you can Google to find a generic PHP proxy script.


EDIT Here is an example of very simple PHP script that will retrieve a specified URL:

<?php readfile($_GET['url']) ?>

Note that you won't be able to POST any data to it, or specify a Content-Type. This is the most basic proxy required for very basic needs.


I understand that JSON is not an option right now but still, here is the explanation of why it can work for cross domain requests.

JSON being Javascript, it can be queried using the <script> tag instead of XMLHttpRequest. Since the <script> tag does not have the same restriction for cross domain request, it is possible to retrieve the JSON content this way.

This technique is called JSONP and is implemented in jQuery in the getJSON function.

这篇关于需要帮助使用JavaScript获取跨域XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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