1

Possible Duplicate:
JQuery ajax cross domain

I need to load content from another websites via ajax. Code below works for localhost urls only but doesn't open urls like google.com

$.ajax({
    url: urlLeft,
    crossDomain:true,
    success: function(data){
        var loadedId = createLoadedPage();
        data = data.replace(/<script.*?>|<\/script>/mgi,'');
        data = data.replace(/.*<body.*?>|<\/body>.*/gi,'');
        $('#'+loadedId).html(data)
        $('#content_left').html($('#'+loadedId+' #snapwrap_outer').html());
        removeLoadedPage(loadedId);
    },
    dataType: 'html'
});
Community
  • 1
  • 1
Oleksandr IY
  • 2,783
  • 6
  • 32
  • 53
  • 3
    http://en.wikipedia.org/wiki/Same_origin_policy? – PeeHaa Mar 30 '12 at 12:26
  • I'm not sure, but i think crossDomain works in such a way that your website server should perform redirect to desired url. – Kane Cohen Mar 30 '12 at 12:27
  • The solution must take account of the rights you have on remote server. You can not use ajax to make requests to a different domain unless you have control to modify server answer (headers) to allow you to do use a remote resource. A solution will be to use a middleman script, a proxy like mechanism to invoke a local ajax call and retrieve remote source with something like cURL. – Paun Narcis Iulian May 04 '18 at 08:46

2 Answers2

3

Due to the same origin policy restriction you cannot send cross domain AJAX requests. Here's a guide that you may take a look for some possible workarounds that you could use.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

That's because of the Same Origin Policy, in your case that basically means that you can't make ajax calls outside of your domain because the browser won't let you. You can't circumvent this.

The solution is to delegate your remote calls to, for example, a PHP script (in the same location as the ajax call that needs to be performed) that will access the data for you, and access that script through an ajax call.

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34