[英]iQuery ajax request returns empty $_POST
我正在創建一個包含上載文件列表的表,我想在最后一列中添加一個鏈接,以允許用戶刪除相應的文件。 我可以簡單地使用鏈接“ delete.php?fileid =“,但是我更喜歡使用POST請求,因此我決定使用一種形式,將數據庫中文件的ID作為隱藏輸入傳遞。
問題 :console.log($(this).closest('form')。find(“:input”)。serialize()); 返回一個空字符串。 您能否幫助了解根本原因?
表單提交代碼:
$('.f-delete a'). click(function(e){
e.preventDefault();
console.log($(this).closest('form').find(":input").serialize());
$.ajax({
type: "POST",
url: 'delete_image.php',
dataType: 'json',
data: $(this).closest('form').find(":input").serialize(),
success: function(response){
console.log(response);
},
error: function(){
alert('Error: Could not delete the file');
}
});
});
標記:
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Type</th>
<th>Size</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Test file</td>
<td>image/png</td>
<td>302.65 KB</td>
<td>
<form id="f-1" class="f-delete" method="post" action="delete_image.php">
<input id="id-1" value="1" type="hidden">
<a id="a-1" href="">Delete image</a>
</form>
</td>
</tr>
<tr>
<td>Test file 2</td>
<td>image/png</td>
<td>37.88 KB</td>
<td>
<form id="f-2" class="f-delete" method="post" action="delete_image.php">
<input id="id-2" value="2" type="hidden">
<a id="a-2" href="">Delete image</a>
</form>
</td>
</tr>
</tbody>
</table>
問題出在您的html中,要使用serialize
您的輸入,應該有名稱。 例如:
<input id="id-1" value="1" name="id-1" type="hidden">
供序列化使用
$(this).closest('form').serialize() //return id-1=1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.