简体   繁体   中英

Fetching content from Website on another Server

What i basically want to do is to get content from a website and load it into a div of another website. This should be no problem so far. The problem is, that the content that should be fetched is located on a different server and i have no source access to it.

I'd prefer a solution using JavaScript of jQuery. Can i use a .htacces redirect to fetch the content from a remote server with client-side (js) techniques?

I will also go with other solutions though. Thanks a lot in advance!

You can't execute an AJAX call against a different domain, due to the same-origin policy. You can add a <script> tag to the DOM which points at a Javascript file on another domain. If this JS file contains some JSON data that you can use, you're all set.

The only problem is you need to get at the JSON data somehow, which is where JSON-P callbacks come into the picture. If the foreign resource supports JSON-P, it will give you something that looks like

your_callback( { // JSON data } );

You then specify your code in the callback.

See JSONP for more.

If JSONP isn't an option, then the best bet is to probably fetch the data server-side, say with a cron job every few minutes, and store it locally on your own site.

You can use a server-side XMLHTTP request to grab your content from the other server. You can then parse it on you server (AKA screen-scraping) and serve-up the portion you want along with your web page.

如果其他网站上的内容只是您要显示在网站上的HTML文档,则也可以使用iframe将其插入。由于浏览器安全性规则,您将无权访问其任何内容。

You will likely have to "scrape" the data you need and store it on your server.

This is a great tutorial on how to cache data from an external site. It is actually written to fetch and store XML, so it'll need some modification. Also, if your site doesn't allow file_get_contents then you may have to modify it to use cUrl .

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