JQuery ajax request

Can anyone tell me what is wrong with this code?

this function will help to delete certain data in the mysql table. The data was successfully deleted, but the response from the jquery ajax function was 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';
    }
});
}
+3
source share
1 answer

AJAX is asynchronous. You cannot get the return data successbecause it is jQuery $.ajaxthat will call it and it does not listen on the return value. AjaxReturnData will exit as soon as the request is sent, and not when it receives a response.

( . "AjaxReturnData" , , , ajax )

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
 });
}
+1

All Articles