简体   繁体   中英

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

then, there is another domain2.com with a data.html page: http://domain2.com/data.html

Is it possible to load data.html into test.html page for processing, and how can I do it?

My dom/javascript knowledge is rather rusty, so I would like to ask what's the way to do it?

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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