Take the 2-minute tour ×

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?

share|improve this question
You can submit a request to your local server, have that retrieve the external page, and then return that in response to the AJAX request. I'm not sure there's a better/easier way to do it. –  Anthony Grist Apr 8 '13 at 15:22
You heard right -- unless you have control of the external domain, and can configure it to allow AJAX from a second domain, then the only way you can use AJAX to retrive data from an external domain is with JSONP. –  Blazemonger Apr 8 '13 at 15:22
@AnthonyGrist Thank you for suggesting another idea, but it sounds tricky. –  Sungam Yang Apr 8 '13 at 15:44
@epascarello Thanks for the link. MDN has many useful documents. –  Sungam Yang Apr 8 '13 at 15:44

4 Answers 4

up vote 6 down vote accepted

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

AFAIK, the only other one that I use often is James Padolseys solution: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

share|improve this answer
Thanks, I decided to use this way. It works well. –  Sungam Yang Apr 8 '13 at 15:35
Here is the link for everyone - jsfiddle.net/hxzyC –  Sungam Yang Apr 8 '13 at 15:43
Wow, the first link is also very useful! Thanks! –  Sungam Yang Apr 8 '13 at 15:48

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.

share|improve this answer
Thanks for your suggestion. By asking a question, I can learn many approaches that I've never thought :) –  Sungam Yang Apr 8 '13 at 15:47

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

share|improve this answer
Use the headers way, it will work best for you. Works with post, get, and json –  PitaJ Apr 8 '13 at 15:24
But if they do not have control of the other domain then it is not possible. –  Joe Frambach Apr 8 '13 at 15:33
It also isn't completely supported in all browsers, IE7 has no support for CORS, and IE8/9 require a plugin (or your own modifications) to use CORS with jQuery's ajax method. –  Kevin B Apr 8 '13 at 15:36
@PitaJ, you mean the cors headers? I don't have permission of the external server. –  Sungam Yang Apr 8 '13 at 15:46

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|improve this answer

Your Answer


