Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get body element from ajax response

The response is a HTML document (from a request from a link's href)

 var data = $(response).find('body').html();
 alert(data);  

 // I get a alert with nothing...

full code:

     $.ajax({
       url: $(this).attr('href'),
       type: 'GET',      
       success: function(response){
         var data = $(response).find('body').html();
         alert(data);  
        }
     });
like image 730
BiberFiver Avatar asked Aug 09 '11 19:08

BiberFiver


People also ask

Are AJAX calls asynchronous?

AJAX's most appealing characteristic is its "asynchronous" nature, which means it can communicate with the server, exchange data, and update the page without having to refresh the page. The two major features of AJAX allow you to do the following: Make requests to the server without reloading the page.

What are AJAX requests?

An AJAX request is a request made by an AJAX application. Typically, it is an HTTP request made by (browser-resident) Javascript that uses XML to encode the request data and/or response data.

What is AJAX how can you use it in your page?

AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

What is AJAX structure?

AJAX = Asynchronous JavaScript And XML. AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)


1 Answers

Try it this way:

var $dom = $(document.createElement("html"));
$dom[0].innerHTML = response;

var $body = $dom.find("body");
like image 122
lamelas Avatar answered Sep 21 '22 01:09

lamelas