简体   繁体   English

jQuery将不提交ajax发布请求

[英]jquery will not submit ajax post requests

Im having aa few javascript gremlins, for some reason my jquery ajax post request is not going out at all. 我有一些javascript gremlins,由于某种原因我的jquery ajax发布请求根本没有退出。 All of my code 'looks' ok, but javascript is not my strong point, can anybody see where im going wrong? 我所有的代码看起来都不错,但是javascript并不是我的强项,有人可以看到我在哪里出错吗?

$(".deleteWeek").click(function(e){
                var answer = confirm("This will delete the selected week. Are you sure?");
                if(answer){
                    $.ajax({
                        type: 'POST',
                        url: 'http://localhost/todo/index.php/home/ajax_delete',
                        data: { page_id : $(this).attr('id') },
                        success: tableRedraw(data),
                        dataType: 'html'
                    })
                }
                e.preventDefault();
              })

and if it helps, my tableRedraw function: 如果有帮助,我的tableRedraw函数:

function tableRedraw(data){
                $('.week').remove(),
                $('thead').after(data) 
            }

my click event is definitely being registered, i can put alerts in handler and they come up alright, but as far as firebug is concerned, there is nothing happening on the ajax side. 我的click事件肯定已被注册,我可以将警报放入处理程序中,然后它们就会出现,但是就Firebug而言,ajax方面没有任何反应。 Any help would be hugely appreciated. 任何帮助将不胜感激。

Your success handler needs to be an anonymous function or a reference to a function. 您的成功处理程序必须是匿名函数或对该函数的引用。

So you should do something like this: 因此,您应该执行以下操作:

$.ajax({
    type: 'POST',
    url: 'http://localhost/todo/index.php/home/ajax_delete',
    data: { page_id : $(this).attr('id') },
    success: tableRedraw,
    dataType: 'html'
});

or 要么

$.ajax({
    type: 'POST',
    url: 'http://localhost/todo/index.php/home/ajax_delete',
    data: { page_id : $(this).attr('id') },
    success: function(data) {
        $('.week').remove(),
        $('thead').after(data) 
    },
    dataType: 'html'
});

This is because the success attribute represents a callback. 这是因为success属性表示回调。 The way you have it right now, the tableRedraw function gets immediately executed, and the result is assigned to success . 按照现在的方式, tableRedraw函数将立即执行,并将结果分配给success This is not what you want. 这不是您想要的。 What you need is a reference to a function (it can be a named function or an anonymous function). 您需要的是对函数的引用(它可以是命名函数或匿名函数)。 That way, when the AJAX request completes successfully, the function is called (hence, callback). 这样,当AJAX请求成功完成时,该函数将被调用(因此,回调)。

You can think of the success attribute as holding a pointer to a function. 您可以将success属性视为持有函数的指针。 Meaning, the entire function itself is being passed around like an object (and basically it is an object because functions in Javascript are first-class). 意思是,整个函数本身就像对象一样被传递(并且基本上它是一个对象,因为Javascript中的函数是一等的)。

You need to use a callback in situations where the operation is asynchronous. 如果操作是异步的,则需要使用回调。 That is, you don't know when the operation will complete. 也就是说,您不知道该操作何时完成。 So basically you tell the operation "Here, take this function and execute it when you are done. Until then I'll go do something else". 因此,基本上,您告诉操作“在这里,请使用此功能并在完成后执行它。在此之前,我将继续执行其他操作”。 This approach is typical when dealing with events (which are asynchronous). 处理事件(异步事件)时,这种方法很典型。

Are you sure it isn't hitting the server or just the callback isn't executing?... 您确定它没有命中服务器或者只是回调未执行?...

Your success callback should be success: tableRedraw, , jQuery will call the function with the return data as the param upon a successful callback. 您的成功回调应该是success: tableRedraw, jQuery将在成功回调后使用返回数据作为参数来调用该函数。 As it is written now, the tableRedraw function is being executed along with the $.ajax() call and the callback is not valid. 如现在所述, tableRedraw函数与$.ajax()调用一起执行,并且回调无效。

IE: IE:

$(".deleteWeek").click(function(e){
    var answer = confirm("This will delete the selected week. Are you sure?");
    if(answer){
        $.ajax({
            type: 'POST',
            url: 'http://localhost/todo/index.php/home/ajax_delete',
            data: { page_id : $(this).attr('id') },
            success: tableRedraw,
            dataType: 'html'
        })
    }
    e.preventDefault();
})

try 尝试

success: tableRedraw,

or 要么

success: function(data) {
  $('.week').remove();
  $('thead').after(data);
},

good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM