Noobie here. I'm writing a client script that needs to read an XML file from another domain. I tried using JSONP. I get a 200 response but the client can't access the returned data for some reason. I get two errors:
Noobie这里。我正在编写一个客户端脚本,需要从另一个域读取XML文件。我试着使用JSONP。我得到了200个响应,但是由于某些原因,客户端无法访问返回的数据。我得到了两个错误:
Resource interpreted as Script but transferred with MIME type text/xml
and
和
Uncaught SyntaxError: Unexpected token <
Here's the code (I've removed the XML url since it's confidential):
下面是代码(我已经删除了XML url,因为它是机密的):
$(document).ready(function() {
$.getJSON("urlOfFilecallback=?", function(data) {
console.log(data)
})
});
When I try to render the data in the console I get:
当我尝试在控制台呈现数据时,我得到:
ReferenceError: data is not defined
How can I fix this? Do I need to use a proxy?
我该怎么解决这个问题呢?我需要使用代理吗?
3 个解决方案
#1
16
You don't have to write your own proxy. You can use YQL if you want to here is an example how:
您不必编写自己的代理。如果你想使用YQL,这里有一个例子:
//sample site that returns xml
site = 'http://goo.gl/9iQWyG';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON(yql, function(data){
console.log(data.results[0]);
});
here is the jsfiddle check console.log.
这是jsfiddle检查console.log。
(Usage limits of the public YQL API is 2,000 requests/hour per IP)
(公共YQL API的使用限制是每IP每小时2000个请求)
#2
1
XML is not allowed for cross-domain requests by default.
默认情况下,不允许XML用于跨域请求。
However, with a little server-side programming you can create a proxy and load the data within your own domain, and output it as XML.
但是,使用少量的服务器端编程,您可以创建代理并在自己的域内加载数据,并将其输出为XML。
for more information see this Question
更多信息请参见这个问题
#3
1
If you have access to the other domain side, you could also use this approach Cross Domain Request
如果您可以访问另一个域,您也可以使用这种方法跨域请求
#1
16
You don't have to write your own proxy. You can use YQL if you want to here is an example how:
您不必编写自己的代理。如果你想使用YQL,这里有一个例子:
//sample site that returns xml
site = 'http://goo.gl/9iQWyG';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON(yql, function(data){
console.log(data.results[0]);
});
here is the jsfiddle check console.log.
这是jsfiddle检查console.log。
(Usage limits of the public YQL API is 2,000 requests/hour per IP)
(公共YQL API的使用限制是每IP每小时2000个请求)
#2
1
XML is not allowed for cross-domain requests by default.
默认情况下,不允许XML用于跨域请求。
However, with a little server-side programming you can create a proxy and load the data within your own domain, and output it as XML.
但是,使用少量的服务器端编程,您可以创建代理并在自己的域内加载数据,并将其输出为XML。
for more information see this Question
更多信息请参见这个问题
#3
1
If you have access to the other domain side, you could also use this approach Cross Domain Request
如果您可以访问另一个域,您也可以使用这种方法跨域请求