Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download content of the page using ajax jquery

Tags:

jquery

ajax

Greetings, how can I download some page content using ajax and jquery: I am doing something like that (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

like image 635
niao Avatar asked May 05 '10 09:05

niao


2 Answers

Due to restrictions you cannot download the contents of a web page using AJAX that is not hosted on the same domain as the domain hosting this script. Also you have a syntax error in your .ajax function call. 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);
    }
});
like image 199
Darin Dimitrov Avatar answered Sep 23 '22 08:09

Darin Dimitrov


You could 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]);
  }
like image 34
Pavlo Avatar answered Sep 26 '22 08:09

Pavlo