Ajax success function
Ajax success function
Hi,
I have created the following AJAX code to retrieve some content via a Kohana PHP callback. The callback function retrieves the content correctly. However, the return statement (highlighted in red) inside the success function does not return the result to the calling point. I am not sure why the following code is not working:
function loadContentLink(href)
{
var mainURL = "http://" + location.host + "/myhome/";
$.ajax({
type: "post",
url: mainURL + "content/load",
data: "id="+href+"&lang="+lang,
dataType: "json",
success: function(result){
return result;
},
error: function(e){
alert('Error: ' + e);
}
});
}
$(document).ready(
function()
{
$("a.quickLink").bind({
click: function(){
var href = $("a.quickLink").attr("href");
}
var content = loadContentLink(href);
$('#tabs-1').html(JSON.decode(content));
}
});
}
);
I have even tried a variation of it which returns the content at the end of the ajax function body (see below) again without success:
function loadContentLink(href)
{
var content="";
var mainURL = "http://" + location.host + "/myhome/";
$.ajax({
type: "post",
url: mainURL + "content/load",
data: "id="+href+"&lang="+lang,
dataType: "json",
success: function(result){
content = result;
},
error: function(e){
alert('Error: ' + e);
}
});
return content;
}
I am not sure why this is not working. I tried to debug the code in Firebug and it turns out that the content variable seems to be undefined.