Can anybody let me know what is wrong with this code?
this function will help to remove a particular data in mysql table. Data was successfully removed but the response from the jquery ajax function is not what I expected.
function removeMember(id,url){
var data = "action=removeMember&id="+id;
var action = ajaxReturnData('POST',data,url);
if(action == '1'){
$(".msg").html("done");
$("#span_delete_"+id).parent().parent().empty().hide("slow");
}else if(action.response == '0'){
$(".msg").html("failed");
}
}
/*User defined functions */
function ajaxReturnData(method,data,url){
$.ajax({
url: url,
data: data,
type: method,
success: function(data) {
return data;
},
error : function(jqXHR, exception){
return '0';
}
});
}
AJAX is asynchronous. You cannot have your success
callback return data, because it is jQuery's $.ajax
that will be calling it, and it is not listening for a return value. AjaxReturnData will exit as soon as the request is sent, not when it receives a response.
You will need to pass along the callbacks (actually, you will need to rethink your logic. "AjaxReturnData" will be a weird method to have now that you know that ajax calls do not return data)
function removeMember(id,url){
var data = "action=removeMember&id="+id;
var action = ajaxReturnData('POST',data,url,function() {
$(".msg").html("done");
$("#span_delete_"+id).parent().parent().empty().hide("slow");
}, function() {
$(".msg").html("failed");
});
}
function ajaxReturnData(method,data,url,onSuccess,onError){
$.ajax({
url: url,
data: data,
type: method,
success: onSuccess,
error : onError
});
}