【发布时间】:2017-08-16 22:06:46
【问题描述】:
我需要使用 jQuery 或 javascript 从第三方获取提要。 我在 jQuery 中有这段代码来读取 jQuery 中的 rss xml 提要
$.ajax({
type: "GET",
url:'https://www.thirdpartysite.com/feed.xml',
dataType: "jsonp",
jsonp: false,
jsonpCallback: 'getFeed',
headers: { "Access-Control-Allow-Origin":"*",},
success: function(xml) {
console.log("success");
console.log(xml);
}
});
function getFeed(val){
console.log(val);
}
这在我的控制台中给了我“Uncaught SyntaxError: Unexpected token
$.ajax({
type: "GET",
url:'http://mytestsite.com/myfeed.php',
dataType: "jsonp",
jsonp: false,
jsonpCallback: 'getFeed',
headers: { "Access-Control-Allow-Origin":"*",},
success: function(xml) {
console.log("success");
console.log(xml);
}
});
function getFeed(val){
console.log(val);
}
myfeed.php 中的代码如下:
echo 'getFeed("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><rss version=\"2.0\"><channel><title>W3Schools Home Page</title><link>https://www.w3schools.com</link><description>Free web building tutorials</description><item><title>RSS Tutorial</title><link>https://www.w3schools.com/xml/xml_rss.asp</link><description>New RSS tutorial on W3Schools</description></item><item><title>XML Tutorial</title><link>https://www.w3schools.com/xml</link><description>New XML tutorial on W3Schools</description></item></channel></rss>");';
似乎我们需要在返回xml时添加回调函数。 但是我无权添加该回调函数
有没有办法让它适用于 xml rss 提要。
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
这个answer 建议搜索另一种方式来提出您的请求(由于 CORS 政策)。您还可以使用以下关键字进行 Google 搜索(如果您还没有):
Uncaught SyntaxError: Unexpected token < +xml。如果可以将响应作为字符串返回并使用JSON.stringify() 转换为 XML 或 json,那就太好了...