Is it possible to load a cross domain web page through AJAX?

10,320

Solution 1

There are many workarounds for this issue (JSONP, reverse proxy, 'Access-Control-Allow-Origin', etc..) described in a very extensive thread here on SO: Ways to circumvent the same-origin policy

Solution 2

The best way of doing this would be to use a local proxy. In other words : do the request server side in a script X , and call this script X from your javascript. That way your "cross-domain" query occurs from the server and there's no restriction to that.

Solution 3

This is possible in two manners.

First, only if the external server has the cors headers set: http://enable-cors.org/server_apache.html. Then you can load it normally. If that header is not set, then it is not possible directly.

Second, you can use the server-side as a proxy. This is really only useful if you are doing GET requests on a static resource. Otherwise the load on the server will be very high. If you do not have a server to use, you can use YQL, but this is sketchy for production usage. http://davescoolblog.blogspot.com/2012/02/client-side-cross-domain-data-yql-hack.html

Solution 4

JSONP allows cross-origin sharing of any Javascript object. A string is a Javascript object, and in your case you could provide the markup for the element to include as a string and use standard Javascript techniques to process it.

If you prefer, you can also use Cross-origin resource sharing, which is less widely supported but doesn't incur the minor JSONP performance hit (although it has other performance flaws like requiring multiple requests for non-GETs).

Share:
10,320
Sungam Yang
Author by

Sungam Yang

To infinity and beyond!

Updated on June 25, 2022

Comments

  • Sungam Yang
    Sungam Yang almost 2 years

    I'm currently developing a mobile web application with jQM.

    Although I looked for answers to my question, I couldn't find any good answers.

    Here is my goal:

    1. Downloading a specific element of a webpage in another domain.
    2. Show the data through a dialog.

    I'm stuck with the first step because of the cross-domain issue.

    Some people writes about using JSONP with a callback function, but it seems that the technique only works when handling JSON format.

    I also read that because of the security issues, JavaScript doesn't support downloading the html page in another domain.

    Are they right?

    There is no way to achieve my goal through JavaScript?