Load page content using ajax jquery

Hi, how can I load some page content using ajax and jquery: I am doing something like this (2 versions inside one script):

$("p").click(function() { $('#result').load('http://google.com'); $.ajax({ url='www.google.com', success: function(data) { $("result").html(data); alert('Load was performed.'); var url = 'www.wp.pl'; $('div#result').load(url); //var content = $.load(url); //alert(content); //$("#result").html("test"); } }); }); 

but it does not return any content

+6
jquery ajax
source share
5 answers

You can use YQL to proxy your call:

 $.ajax({ url:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http://www.google.com'&format=xml&callback=callback", type: 'GET', dataType: 'jsonp' }); function callback(data){ $('#result').html(data.results[0]); } 
+7
source share

Due to limitations, you cannot load the contents of a web page using AJAX, which is not hosted in the same domain as the domain that hosts this script. You also have a syntax error in calling the .ajax function. It should look like this:

 $.ajax({ url: 'http://yourdomain.com/page1.htm', success: function(data) { $("result").html(data); alert('Load was performed.'); var url = 'http://yourdomain.com/page2.htm'; $('div#result').load(url); } }); 
+7
source share

While it is not possible to directly request a host that is external to the current domain from Javascript, you can use a proxy script to retrieve the desired data.

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

You can also use the flXHR script, which can be dropped into many Javascript libraries (including jQuery).

flXHR: http://flxhr.flensed.com/

+2
source share

You can also simply call up the PHP / ASP / RUby page, which in turn will call you out and provide you with the information as you need.

 1. PAGE --> PHP --> External web (Ajax) 2. PAGE <-- PHP <-- External web (callback) 
+1
source share

You need to use something called JSONP to navigate through the domain. Seider talked in detail about how to do this using jQuery.

+1
source share

All Articles