最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cross-Domain AJAX to Read XML - Stack Overflow

programmeradmin2浏览0评论

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

and

Uncaught SyntaxError: Unexpected token <

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?

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

and

Uncaught SyntaxError: Unexpected token <

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?

Share Improve this question asked Aug 16, 2013 at 18:09 Ben DavidowBen Davidow 1,2155 gold badges27 silver badges55 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

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

here is the jsfiddle check console.log.

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

XML is not allowed for cross-domain requests by default.

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.

for more information see this Question

If you have access to the other domain side, you could also use this approach Cross Domain Request

发布评论

评论列表(0)

  1. 暂无评论