简体   繁体   English

JavaScript 可以从第三方域加载 XML 数据吗?

[英]Can JavaScript load XML data from a third-party domain?

Can JavaScript load an RSS XML feed from Yahoo ? JavaScript 可以从Yahoo加载 RSS XML 提要吗?

Is client-side JS allowed to access 3rd-party domains?客户端 JS 是否允许访问第三方域?

You can use the Google Feed API to load RSS in JavaScript from any domain / server.您可以使用Google Feed API 从任何域/服务器加载 JavaScript 中的 RSS。 More than just a proxy, it actually serves the RSS content from the Google cache instead of hitting the original server.它不仅仅是一个代理,它实际上从 Google 缓存中提供 RSS 内容,而不是访问原始服务器。 This could be a lifesaver for small servers that can't handle Slashdot traffic surges.对于无法处理 Slashdot 流量激增的小型服务器来说,这可能是救命稻草。

I used the Feed API for a cross-site RSS access in an article on Silverlight several years ago.几年前,我在一篇关于 Silverlight的文章中使用了 Feed API 进行跨站点 RSS 访问。

An easy way to do this is to proxy the request through the server that your page resides on.一种简单的方法是通过页面所在的服务器代理请求。 Steps are:步骤是:

  1. Write a server side script performs an http request on the rss feed, when that script itself is request (ie via get or post)编写服务器端脚本在 rss 提要上执行 http 请求,当该脚本本身是请求时(即通过 get 或 post)
  2. Use ajax to request the server side script, or just call it from the main script for that page.使用 ajax 请求服务器端脚本,或者只是从该页面的主脚本中调用它。
  3. The server side script then returns the feed source in some displayable form.然后,服务器端脚本以某种可显示的形式返回提要源。
  4. Profit!利润!

On IE 8 and FF 3.1(not certain), it is possible to make these requests through specialized cross site calls, but the last generation of browsers will still cause problems.在 IE 8 和 FF 3.1(不确定)上,可以通过专门的跨站点调用发出这些请求,但上一代浏览器仍然会出现问题。 See:看:

http://dannythorpe.com/2009/01/15/ie8-cross-domain-request-support-demo/ http://ejohn.org/blog/cross-site-xmlhttprequest/ Feature is restricted in FF 3.0, unclear if it will be back in 3.1 http://dannythorpe.com/2009/01/15/ie8-cross-domain-request-support-demo/ http://ejohn.org/blog/cross-site-xmlhttprequest/功能在 FF 3.0 中受到限制,如果它会回到 3.1

However, the steps above are guaranteed not to run afoul of any browser CSS security, at the expense of some lag and extra hw load on your server.但是,保证上述步骤不会与任何浏览器 CSS 安全性发生冲突,代价是服务器上的一些延迟和额外的硬件负载。

You can use the technique outlined in my blog post Unwritten guide to Yahoo Query Langauge您可以使用我的博客文章未成文指南雅虎查询语言中概述的技术

You would query the XML data table with a yql statment like this:您将使用如下 yql 语句查询 XML 数据表:

select * from xml
  where url="http://path/to/xml
Then you would add a script tag to your html (can be done with document.createElement('script')) with a src http://query.yahooapis.com/v1/public/yql?q={your yql here}&format=json&callback={your function here} where {your yql here} is replace with a URI Encoded version of you yql statment. 然后,您将使用 src http://query.yahooapis.com/v1/public/yql?q={your yql here} 向 html 添加一个脚本标签(可以使用 document.createElement('script')) &format=json&callback={your function here} 其中 {your yql here} 替换为 yql 语句的 URI 编码版本。

I'm not sure about JS but I know that you can use one of google's APIs and they have an RSS reader.我不确定 JS,但我知道您可以使用google 的 API之一,并且他们有一个 RSS 阅读器。 I know this probably isn't what you want, but if you read through the documentation you may be able to get your answer on how it works.我知道这可能不是您想要的,但是如果您通读文档,您可能会得到关于它是如何工作的答案。

Not directly.不是直接的。 You can use Dana's suggestion of proxing the request, or look into a method called JSONP, which essentially wraps the returned JSON object in a custom callback function, requested by a script tag you inject into your DOM.您可以使用 Dana 提出的代理请求的建议,或者查看一种称为 JSONP 的方法,该方法实质上将返回的 JSON object 包装在自定义回调 ZC1C425268E68385D1AB544F 请求的脚本标签中。 Most API providers support this (including Yahoo's APIs).大多数 API 提供商都支持这一点(包括 Yahoo 的 API)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM