Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display html code of response returned by ajax, Jquery

I have a jquery AJAX function which retrieves some HTML markup and displays it on the page. I would also like to display the html code of this HTML returned. I've looked around for a solution but not finding any. Can someone please help. Many thanks

$.post('get_news.php', $("#gifForm").serialize(), function(data) {
     //Show HTML
     $('#output').html(data);

     //Show HTML code
     $('#output_code').html(data);
});
like image 525
user1038814 Avatar asked Jan 17 '12 19:01

user1038814


People also ask

Can ajax return html?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

What is returned by ajax?

$. ajax() (and related convenience methods) returns a jqXHR object — a jQuery XML HTTP Request — which has a host of powerful methods. We can make a request using $. ajax() , and then capture the returned jqXHR object in a variable.


2 Answers

try using the text() function. This will escape and display html code. http://api.jquery.com/text/

$.post('get_news.php', $("#gifForm").serialize(), function(data) {
  //Show HTML
  $('#output').html(data);
  //Show HTML code
  $('#output_code').text(data);
});
like image 200
Hoppy Avatar answered Oct 30 '22 10:10

Hoppy


You can surround your html with "code" and it should display it as is without rendering the html:

$('#output_code').html("<code>" + data + "</code>");
like image 36
Troy Barlow Avatar answered Oct 30 '22 11:10

Troy Barlow