1
我正在使用MVC,並且我有一個按鈕使用AJAX調用來刪除站點上傳的圖像。PHP從控制器返回值到Ajax
這是我的模型:
public function remove_document($documentID, $documentName)
{
$objData = $this->objDB
-> setStoredProc('attritionRemoveDocument')
-> setParam('documentID', $documentID)
-> setParam('documentName', $documentName)
-> execStoredProc()
-> parseXML();
return $objData->data->response;
}
響應從這個回來或者是true
或false
。
這裏是我的控制器:
public function deleteFile()
{
// Get the documentID we are removing
$documentID = $this->input->post('documentID');
$documentName = $this->input->post('documentName');
// Check if the file is even there
if (file_exists('./uploads/'.$documentName)){
// Remove file
unlink('./uploads/'.$documentName);
$removeFile = $this->submit_model->remove_document($documentID, $documentName);
return $removeFile;
}
}
最後,我的AJAX調用:
$('[name=deleteDocument]').click(function() {
var documentID = $(this).attr('documentID'),
documentName = $(this).attr('documentName');
//Delete the image
$.ajax({
type: 'POST',
url: '../deleteFile',
dataType: 'xml',
data: {
'documentID': documentID,
'documentName': documentName
},
success: function(msg) {
// On Success, remove the current file section
console.log(msg);
}
});
});
當我echo
在控制器中$removeFile
價值,我看到了true/false
值但它永遠不會使它到AJAX調用的成功功能。
任何想法?
Ajax無法檢索'PHP'函數的返回值。你需要使用'echo'來代替。 – 2014-10-10 16:58:23
我改變它回聲,但'console.log(味精);'仍然不顯示值。還有什麼我需要添加到'味精'? – SBB 2014-10-10 17:10:49
您在ajax調用中的數據類型屬性期望從服務器返回XML,這就是爲什麼我以我的方式回答問題的原因。嘗試將其更改爲'html' – Overachiever 2014-10-10 18:26:29