Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery / Ajax fetches local but not remote pages [duplicate]

Tags:

jquery

ajax

Possible Duplicate:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

I still don't get why this code works fine when loading local server pages (localhost) but display nothing when trying to fetch remote data

$(document).ready(function(){

    $.get(
    "message.html",
    function(data) { $("div").html(data); },
    "html"
    );
});

and displaying the remote html file gives me no error but no data:

$(document).ready(function(){

    $.get(
    "http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html",
    function(data) { $("div").html(data); },
    "html"
    );
});

Regards

like image 408
Kandinski Avatar asked Sep 01 '25 01:09

Kandinski


1 Answers

There is a built-in restriction to prevent cross-domain Ajax requests from the browser. Pretty much all browsers implement this.

There are workarounds such as the article here or using JSONP. But this is a basic restriction put on Ajax requests sent from the browser. If you are using ASP.NET Encosia also had a good tip on doing the proxying using a custom HttpHandler.

See also: Cross Domain Limitations With Ajax - JSON

like image 99
njr101 Avatar answered Sep 02 '25 17:09

njr101