i have a problem with my code.
function fnFormatDetails ( oTable, nTr )
{
var aData = oTable.fnGetData( nTr );
var sOut = "";
var ajax = $.ajax({
url: "/wemi/mediaplaner/show?id="+aData[1],
success: function(data) {
//return data;
//return sOut = data;
//console.log(sOut);
},
error: function() {
//alert("Error");
}
});
//document.write(ajax.responseText); //write empty Text
console.log(ajax); //Real Object all is OK
//console.log(ajax.responseText); get empty Text
//alert(ajax.responseText); // get Empty Text
return sOut;
}
I just need to print the ajaxed text… and as you can see i get only undifined or empty response.
but if i print or alert in success function, all is Ok and i get my response…
You’ve discovered the answer already. The ajax call is asynchronous. The code in the “success” handler will be executed when the HTTP request completes. The
$.ajaxfunction itself, however, returns immediately after starting the request.The correct approach is to put the code in the “success” callback. (Or put the call to some other function there.)