I am making the following jquery ajax call to a codeigniter php function:
var html ="";
$.ajax({
type:"POST",
url: "Ajax/getHtml",
data: { u : contents },
dataType: 'html',
success: function(data) {
html = data;
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('error');
console.log(jqXHR,textStatus, errorThrown);
}
});
console.log('html', html);
This is working correctly and html is being returned on success which I can see if I log 'data' to the console. However I don't seem to be able to capture the HTML response in a javascript variable. I have declared one (html) globally. When I look at the console I see:
html
jquery-2.1.1.js:8623 XHR finished loading: POST "http://localhost/b1/Ajax/getHtml".
I'm not experienced with javascript but this seems out of order, so I think the problem is that javascripts asynchronous nature causes the script to move forward without waiting for the ajax request to return results.
Does this make sense and how should I fix this?