这个getJson调用有什么问题?

whats wrong with this getJson call

本文关键字:问题 什么 getJson 调用 这个      更新时间:2023-09-26

这是我的jQuery

$.getJSON('/users/delete_preapproval', { 
  preapproval: $(this).attr('id'), id: $(this).attr('id') }, 
function(data) {
complete: function(){
     console.log('ssss');
  }
});

一切正常,服务器处理请求,但完成不工作…我没有得到我的console.log显示。我基本上需要一个on complete或on success来触发一个成功的ajax调用

编辑:这是我整个点击事件:

$('.delete_preapproval').click(function(e){
    var count = $(this).closest('.request_count').attr('data');
    if(count > 0){
        if(count == 1){
            var plural = 'request';
        }else{
            var plural = 'requests';
        }           
        if (confirm('Are you sure you want to delete this preapproval you have ' + count + ' active ' + plural)){
                $.getJSON('/users/delete_preapproval', { 
                    preapproval: $(this).attr('id'), 
                    id: $(this).attr('id') 
                }, 
                function(data) {
                     console.log('ssss');
                });
     }else{
            return false;
        }
    }else{
        $.getJSON('/users/delete_preapproval', { preapproval: $(this).attr('id'), id: $(this).attr('id') }, function(data) {
      window.location.reload();
        });
    }
    e.preventDefault();
});

您实际上应该得到一个语法错误。删除complete::

$.getJSON('/users/delete_preapproval', { 
    preapproval: $(this).attr('id'), 
    id: $(this).attr('id') 
}, 
function(data) {
     console.log('ssss');
});

complete:在这一行创建了一个标签[docs]。删除标签将是:

function(data) {
    function(){
        console.log('ssss');
    } 
}

您可以看到,当调用外部函数时,内部函数永远不会执行(除了语法错误)。

Update:还要确保返回的数据是正确的JSON。否则,jQuery无法解析它,也不会调用回调。

您需要删除完整的:和它后面的函数调用。比如:

$.getJSON('/users/delete_preapproval', { 
  preapproval: $(this).attr('id'), id: $(this).attr('id') }, 
function(data) {
     console.log(data);
});

你为什么不试试呢

$.ajax({
    type: 'post',
    url: "/users/delete_preapproval",           
    dataType: 'json',
  data: {preapproval : $(this).attr('id'), id : $(this).attr('title')},
    complete: function () { 
        console.log('ssss');
    }           
});