这个 getJson 调用有什么问题

发布于 2024-11-25 16:27:13 字数 1265 浏览 0 评论 0原文

这是我的 jQuery

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

All 运行良好,服务器处理请求,但完整的不起作用...我没有显示我的 console.log。我基本上需要在成功的 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();
});

here is my jQuery

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

All works well and the server processes the request but the complete is not working...I am not getting my console.log displaying. I basically need an on complete or on success to fire after a successful ajax call

EDIT: here is my entire click event:

$('.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();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风也等你 2024-12-02 16:27:13

你实际上应该得到一个语法错误。删除 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');
    } 
}

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

更新:还要确保您返回的数据是正确的 JSON。否则 jQuery 无法解析它并且不会调用回调。

You should actually get a syntax error. Remove complete: :

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

complete: is creating a label [docs] at this line. Removing the label would be:

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

You see that that inner function is never executed when the outer function is called (apart from the syntax error).

Update: Also make sure that the data you return is proper JSON. Otherwise jQuery cannot parse it and will not call the callback.

蒲公英的约定 2024-12-02 16:27:13

您需要删除完整的:及其后面的函数调用。所以更像是:

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

You need to remove the complete: and the function call after it. So something more like:

$.getJSON('/users/delete_preapproval', { 
  preapproval: $(this).attr('id'), id: $(this).attr('id') }, 
function(data) {
     console.log(data);
});
风为裳 2024-12-02 16:27:13

你为什么不尝试一下

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

那应该可行...

Why don't you try

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

That should work...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文