I get undefined for some reason when I try to return html via a callback function:
function getDataFromUrl(urlWithContent) { // jQuery async request $.ajax( { url: urlWithContent, dataType: "html", success: function(data) { return $('.result').html(data); }, error: function(e) { alert('Error: ' + e); } }); }
I know that I get the data back, I see this in firebug in the response, and also when I warn the data, I see that the contents of the entire page appears in the warning window.
When I call my function, I do the following:
var divContent = getDataFromUrl(dialogDiv.attr("href")); if(divContent) dialogDiv.innerHTML = divContent;
when I warn divContent (before the if statement), I get undefined. Maybe I'm just mistaken in how I return the data?
I also tried just to return the data; The same thing, I get undefined after calling this method when setting my variable.
Updated for replies:
Tried this while still getting undefined:
function getDataFromUrl(urlWithContent, divToUpdate) { $.ajax( { url: urlWithContent, aSync: false, dataType: "html", success: function(data) { divToUpdate.innerHTML = data; }, error: function(e) { alert('Error: ' + e); } }); }
I called it from another function, for example:
var divContent = ""; if (dialogDiv.attr("href")) { getDataFromUrl(dialogDiv.attr("href"), divContent); }