本文介绍了跨域AJAX读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

Uncaught SyntaxError: Unexpected token <

以下代码(我已删除XML网址,因为它是保密的):

Here's the code (I've removed the XML url since it's confidential):

$(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?

推荐答案

您不必编写自己的代理。你可以使用YQL,如果你想在这里是一个例子如何:

You don't have to write your own proxy. You can use YQL if you want to here is an example how:

//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]);
});

这里是检查console.log。

here is the jsfiddle check console.log.

(为每个IP 2000请求/小时)

(Usage limits of the public YQL API is 2,000 requests/hour per IP)

这篇关于跨域AJAX读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:44