3

Possible Duplicate:
load content from external page into another page using ajax/query

May be I just don't get something.

I want to get html page from other site, let it be http://www.server.com/somepage/param

In my js code:

  var url = "http://www.server.com/somepage/param";
   $.get(url, callback);

Chrome says "Failed to load resource".

What is wrong?

Community
  • 1
  • 1
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181

2 Answers2

6

The simple answer is, no, this is not possible. Cross Domain AJAX is not allowed. However, you can find a (working) workaround here:

http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html

More details about cross-domain ajax requests: Dashboard Cross-domain AJAX with jquery

Community
  • 1
  • 1
ChrisH
  • 1,283
  • 1
  • 9
  • 22
  • Well, but I've got a chrome extension, and it makes such requests, and get the information from site I need. How? I looked at js source it uses jQuery. – WHITECOLOR Oct 02 '11 at 23:38
6

You're running into restrictions imposed by the Same Origin Policy. In short, AJAX calls to a different domain are prohibited and will always fail.

You need to either use JSONP (mostly applicable to data returned by APIs) or proxy the request through your own server/domain.

vzwick
  • 11,008
  • 5
  • 43
  • 63
  • Well, but I've got a chrome extension, and it makes such requests, and get the information from site I need. How? I looked at js source it uses jQuery. – WHITECOLOR Oct 02 '11 at 23:38
  • 2
    It uses special permissions given to Plugins. Here's an excellent writeup about how to implement this: http://www.sarajchipps.com/2011/01/using-jquery-ajax-calls-in-a-chrome-plugin.html – vzwick Oct 02 '11 at 23:41
  • Oh, thank you very much for your answer. Sadly that, this simple solution works only in crhome, It seems that I need to find a way to use proxy solution with my ROR 3.1 server – WHITECOLOR Oct 02 '11 at 23:56
  • But what about this document: http://www.w3.org/TR/cors/ (infromation from here http://merbist.com/2011/09/14/how-to-cross-domain-ajax-in-a-ruby-app/ - However, most browsers (IE 8+, Firefox 3.5+, Safari 4+, Chrome) implement a simple way to allow cross domain requests as defined in this w3C document.) – WHITECOLOR Oct 03 '11 at 00:04
  • Well, can you get the remote host to send a `Access-Control-Allow-Origin: http://yourhost.com` header? – vzwick Oct 03 '11 at 00:08
  • Oh, I believe no =( so the only way it make proxy on my server? – WHITECOLOR Oct 03 '11 at 00:18
  • Yap. Shouldn't be too complicated though, should it? In PHP, that'd be a three-liner. – vzwick Oct 03 '11 at 00:23