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);
});
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.
$. 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.
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);
});
You can surround your html with "code" and it should display it as is without rendering the html:
$('#output_code').html("<code>" + data + "</code>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With