簡體   English   中英

jQuery將不提交ajax發布請求

[英]jquery will not submit ajax post requests

我有一些javascript gremlins,由於某種原因我的jquery ajax發布請求根本沒有退出。 我所有的代碼看起來都不錯,但是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();
              })

如果有幫助,我的tableRedraw函數:

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

我的click事件肯定已被注冊,我可以將警報放入處理程序中,然后它們就會出現,但是就Firebug而言,ajax方面沒有任何反應。 任何幫助將不勝感激。

您的成功處理程序必須是匿名函數或對該函數的引用。

因此,您應該執行以下操作:

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

要么

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

這是因為success屬性表示回調。 按照現在的方式, tableRedraw函數將立即執行,並將結果分配給success 這不是您想要的。 您需要的是對函數的引用(它可以是命名函數或匿名函數)。 這樣,當AJAX請求成功完成時,該函數將被調用(因此,回調)。

您可以將success屬性視為持有函數的指針。 意思是,整個函數本身就像對象一樣被傳遞(並且基本上它是一個對象,因為Javascript中的函數是一等的)。

如果操作是異步的,則需要使用回調。 也就是說,您不知道該操作何時完成。 因此,基本上,您告訴操作“在這里,請使用此功能並在完成后執行它。在此之前,我將繼續執行其他操作”。 處理事件(異步事件)時,這種方法很典型。

您確定它沒有命中服務器或者只是回調未執行?...

您的成功回調應該是success: tableRedraw, jQuery將在成功回調后使用返回數據作為參數來調用該函數。 如現在所述, tableRedraw函數與$.ajax()調用一起執行,並且回調無效。

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

嘗試

success: tableRedraw,

要么

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

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM