我在一个名为“add.cpt”的页面中,其中包含图像列表。用户可以选择删除图像,但我无法使其工作。在单击的情况下,我尝试调用 ajax 尝试传递图像的名称和项目的 id (.../item/imageName),但它确实删除了图像并提醒似乎是 delete_photo_file 的内容。 ctp。看起来 ajax 正在使用 URL,但它没有发送数据来删除想要的文件。
物品控制器:
App::uses('File', 'Utility');
class ItemsController extends AppController{
[...]
public function deletePhotoFile(){
//$this->autoRender = false; //did not tested but maybe I need to use this
$imgName = //don't know how to get it from ajax call
$itemId = //don't know how to get it from ajax call
$file = new File($dir.'/'.$itemId.'/'.$imgName);
$file->delete();
}
}
Ajax 调用(来自我的 ctp 文件):
$('#delete').click(function (){
[...]
var itemId=$('#itemId').val(); //comes from hidden input
var imgName = $('#imgName').val(); //comes from hidden input
$.ajax({
type: 'POST',
url:'http://localhost/html/Project/v5/CakeStrap/items/deletePhotoFile/',
data:{"itemId":itemId, imgName: imgName},
success: function(data){
alert(data); //alerts some HTML... seems to be delete_photo_file.ctp content
}
});
});
谁能帮我?谢谢!