2016-01-24 38 views
0

有問題。每個顯示的圖像都有兩個不同的按鈕。一個是刪除,另一個是指定爲「主要」。成功不起作用的jQuery AJAX函數

刪除作品。它隱藏圖像,刪除文件和MySQL行。

指定主類作品。它會更新數據庫中將「main」值更改爲1的行,但它應該也會alert(),但它不會。

<script> 

$(document).ready(function() { 
    $(".remove_image").click(function() { 

     var image_id = $(this).attr('id'); 

     $.ajax({ 
      type:"post", 
      url:"imagecontrol.php", 
      data: { image_id:image_id, 
        image_remove:1}, 
      success: function(response) { 
       $('#image_'+image_id).fadeOut(400); 
       showUploader(); 
      } 
     }) 

    }) 
}); 

$(document).ready(function() { 
    $(".assign_main").click(function() { 

     var assign_this_id = $(this).attr('id'); 

     $.ajax({ 
      type:"post", 
      url:"imagecontrol.php", 
      data: { assign_this_id:assign_this_id, 
        image_assign:1}, 
      success: function(response) { 
       alert("Success"); 
      } 
     }) 
    }) 
}); 

</script> 
+3

添加一個錯誤處理程序,看看它是否被觸發。 – epascarello

+2

檢查php日誌。可能有一個錯誤導致進程失敗。 –

+2

您的服務器很可能告訴瀏覽器請求失敗,即使它已成功地在數據庫中進行更新。 – fzzfzzfzz

回答

-1

使用done(),而不是它:

$.ajax({ 
    type: "post", 
    url: "imagecontrol.php", 
    data: { 
     image_id: image_id, 
     image_remove: 1 
    } 
}).done(function(response) { 
    $('#image_' + image_id).fadeOut(400); 
    showUploader(); 
); 
+1

如果成功回調沒有被觸發,爲什麼這會有所幫助? – charlietfl

0

success方法只有一個返回HTTP 200HTTP 304時調用,因此您可能需要仔細檢查,看看是否是真正的情況。

這可以在大多數現代網絡瀏覽器的「檢查器」面板中完成,通常在「網絡」選項卡下。

您還可以添加一個error: function() {}事件處理程序來捕獲任何HTTP 4xx/5xx代碼。

-1

嘗試,並提醒錯誤,肯定是存在的:

$(document).ready(function() { 
    $(".assign_main").click(function() { 

     var assign_this_id = $(this).attr('id'); 

     $.ajax({ 
      type:"post", 
      url:"imagecontrol.php", 
      data: { assign_this_id:assign_this_id, 
       image_assign:1}, 
      success: function(response) { 
       alert("Success"); 
      }, 
      error: function(jqXHR, textStatus, errorThrown) { 
       alert(textStatus + " " + errorThrown); 
      } 
     }) 
    }) 
}); 

這會告訴你,如果有什麼問題在PHP代碼