简体   繁体   English

html:从其他域加载外部文档

[英]html: load an external document from from different domain

Suppose, I have a domain1.com, there I have an html page: http://domain1.com/test.html 假设我有一个domain1.com,那里有一个html页面: http : //domain1.com/test.html

then, there is another domain2.com with a data.html page: http://domain2.com/data.html 然后,有一个data.html页面另一个domain2.com: http://domain2.com/data.html

Is it possible to load data.html into test.html page for processing, and how can I do it? 是否可以将data.html加载到test.html页面进行处理,我该怎么做?

My dom/javascript knowledge is rather rusty, so I would like to ask what's the way to do it? 我的dom / javascript知识还很生锈,所以我想问问这是怎么做的?

Basically, I want to load it into some hidden frame or using xml request (or whatever it's called), and then process that data using JS. 基本上,我想将其加载到某个隐藏的框架中或使用xml请求(或任何它所谓的名称),然后使用JS处理该数据。 This should work on regular current browsers and shouldn't require any extra settings to allow cross domain requests (if there is an issue with that). 这应该可以在当前的常规浏览器上运行,并且不需要任何其他设置即可允许跨域请求(如果存在问题)。

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. 如果远程服务器和浏览器支持, 跨域资源共享(CORS)将是简单,直接的解决方案。 You'd need a bit of "extra settings" on the server side. 您需要在服务器端进行一些“额外设置”。 Afterwards, use AJAX with modified headers to allow CORS. 之后,将AJAX与经过修改的标头一起使用以允许CORS。

  • JSON-P which is a JS object wrapped in a callback. JSON-P是包装在回调中的JS对象。 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. 您需要确定接收回调的名称,将数据编码为JSON并将JSON包装在函数调用中。 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). 您可能想让您域上的服务器脚本从另一个域(例如wget或curl)加载页面。 Since you request your server to load the page for you, you only need the usual AJAX on the client-domain part. 由于您请求服务器为您加载页面,因此您只需要在客户端域部分使用通常的AJAX。

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

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