weixin_33686714 2014-02-07 09:30 采纳率: 0%
浏览 22

jQuery Ajax请求

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';
    }
});
}
  • 写回答

1条回答 默认 最新

  • weixin_33682790 2014-02-07 09:33
    关注

    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
     });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程