How to get content from another domain using .load ()?

Querying data from any location in my domain using .load () (or any jQuery ajax functions) works fine.

Trying to access a URL in another domain does not work. How do you do this? Another domain is also mine.

I read about the trick you can do with PHP and create a proxy server that receives content, and then you use the ajax jQuery functions in this php place on your server, but still use jQuery ajax on your own server, 't count

Is there a good plugin?

EDIT:. I found a very good jQuery plugin that allows you to request content from other pages using any of the jQuery functions, just like a normal normal ajax request in your own domain.

Post: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

Plugin: https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

+7
source share
4 answers

This is because of the cross-domain policy, which in a way means that using the client side of the script (aka javascript ...) you cannot request data from another domain. Fortunately for us, this limitation does not exist in most server-side scenarios.

So...

JavaScript:

$("#google-html").load("google-html.php"); 

PHP in "google-html.php":

 echo file_get_contents("http://www.google.com/"); 

will work.

+10
source

Different domains = different servers depending on your browser. Either use JSONP to execute the request, or use PHP for the proxy. You can use jQuery.ajax() to execute a cross-domain JSONP request.

+3
source

One very simple way is to use the Yahoo YQL service, which can retrieve content from any external site.

I have successfully done this on several sites following this example, which uses only JavaScript and YQL. http://icant.co.uk/articles/crossdomain-ajax-with-jquery/using-yql.html

This example is part of a blog post that also outlines some other solutions. http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/

+3
source

I know another solution that works. This does not require a jQuery change. This requires that you can access the ASP page in your domain. I myself used this method.

1) Create a proxy.asp page similar to the one on this page http://www.itbsllc.com/zip/proxyscripts.html

2) Then you can execute the jQuery download function and pass it to proxy.asp? url = ....... there is an example of exactly how to format it. In any case, you upload the URL of the external page and the desired mime type as variables to your local proxy.asp page. The two types of mime that I used are text / html and image / jpg.

Please note that if your landing page has images with relative source links that are likely to not load. Hope this helps.

+1
source

All Articles