gpt4 book ai didi

javascript - 跨域AJAX读取XML

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:46 24 4
gpt4 key购买 nike

这里是新手。我正在编写需要从另一个域读取 XML 文件的客户端脚本。我尝试使用 JSONP。我收到 200 响应,但客户端出于某种原因无法访问返回的数据。我收到两个错误:

Resource interpreted as Script but transferred with MIME type text/xml

Uncaught SyntaxError: Unexpected token <

这是代码(我已经删除了 XML url,因为它是 secret 的):

$(document).ready(function() {
$.getJSON("urlOfFilecallback=?", function(data) {
console.log(data)
})
});

当我尝试在控制台中呈现数据时,我得到:

ReferenceError: data is not defined

我该如何解决这个问题?我需要使用代理吗?

最佳答案

您不必编写自己的代理。如果您愿意,可以使用 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]);
});

这里是 jsfiddle检查 console.log。

(公共(public) YQL API 的 Usage limits 是每个 IP 每小时 2,000 个请求)

关于javascript - 跨域AJAX读取XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com