0

假设,我有一个 domain1.com,那里有一个 html 页面: http ://domain1.com/test.html

然后,还有另一个带有 data.html 页面的 domain2.com:http://domain2.com/data.html

是否可以将 data.html 加载到 test.html 页面中进行处理,我该怎么做?

我的dom / javascript知识比较生疏,所以我想问一下这样做的方法是什么?

基本上,我想将它加载到某个隐藏的框架中或使用 xml 请求(或任何它的名称),然后使用 JS 处理该数据。这应该适用于常规的当前浏览器,并且不需要任何额外的设置来允许跨域请求(如果存在问题)。

4

1 回答 1

0

Normally, you can't. But you have options, and a bit of "extra settings"

  • Cross-Origin Resource sharing (CORS) would be the easy, straight-forward solution, if the remote server and the browser supports it. You'd need a bit of "extra settings" on the server side. Afterwards, use AJAX with modified headers to allow CORS.

  • JSON-P which is a JS object wrapped in a callback. Still, some more extra settings on the server. You need to determine the receiving callback's name, encode the data in JSON and wrap the JSON in a function call. Afterwards load it in a dynamic script tag.

  • A server proxy. This has the most "extra settings" but grants you more flexibility. You would want to ask a server script on your domain to load a page from the other domain (something like wget or curl). Since you request your server to load the page for you, you only need the usual AJAX on the client-domain part.

于 2012-05-19T15:19:59.717 回答