我在學習Zend框架2.我發現了一個關於如何在zend中創建Jquery模式窗口ajax請求的文章。我做了一個彈出窗口,用戶可以添加專輯名稱和專輯類型。 當我在以下運行腳本時,沒有 Jquery模式窗口它完全保存到我的數據庫中,但是當我試圖通過ajax來做到這一點時,什麼都沒有進入數據庫。我開始尋找錯誤,我在控制檯發現當我要發佈形式:zend框架2 ajax請求錯誤
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost/album/validatepostajax
,但是當我點擊網址加載。
任何幫助,非常感謝。
控制器:
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AddNewAlbumController extends AbstractActionController {
public function savetodb($data)
{
$mapper = $this->getServiceLocator()->get('Album/Mapper/Album');
$mapper->insert($data);
}
protected function getForm()
{
$form = $this->getServiceLocator()->get('Album\Form\AddNewAlbumForm');
$form->setInputFilter(new \Album\Form\Filters\AddNewAlbumFormFilter());
$form->setName('AlbumForm');
return $form;
}
public function newalbumAction()
{
$viewmodel = new ViewModel();
$viewmodel->setTemplate('album/form/add-album.phtml');
$form = $this->getForm();
$form->setHydrator (new \Zend\Stdlib\Hydrator\Reflection());
$request = $this->getRequest();
//disable layout if request by Ajax
$viewmodel->setTerminal($request->isXmlHttpRequest());
$is_xmlhttprequest = 1;
if (! $request->isXmlHttpRequest()){
//if NOT using Ajax
$is_xmlhttprequest = 0;
if ($request->isPost()){
$form->bind (new \Album\Entity\Album());
$form->setData($request->getPost());
$mapper = $this->getServiceLocator()->get('Album/Mapper/Album');
if ($form->isValid()){
$this->savetodb($form->getData());
}
}
}
$viewmodel->setVariables(array(
'form' => $form,
'is_xmlhttprequest' => $is_xmlhttprequest //need for check this form is in modal dialog or not in view
));
return $viewmodel;
}
public function validatepostajaxAction()
{
$form = $this->getForm();
$form->setHydrator (new \Zend\Stdlib\Hydrator\Reflection());
$request = $this->getRequest();
$response = $this->getResponse();
$messages = array();
if ($request->isPost()){
$form->bind (new \Album\Entity\Album());
$form->setData($request->getPost());
if (! $form->isValid()) {
$errors = $form->getMessages();
foreach($errors as $key=>$row)
{
if (!empty($row) && $key != 'submit') {
foreach($row as $keyer => $rower)
{
$messages[$key][] = $rower;
}
}
}
}
if (!empty($messages)){
$response->setContent(\Zend\Json\Json::encode($messages));
} else {
$this->savetodb($form->getData());
$response->setContent(\Zend\Json\Json::encode(array('success'=>1)));
}
}
return $response;
}
}
檢視:
<script type="text/javascript">
var is_xmlhttprequest = <?php echo $this->is_xmlhttprequest; ?>;
var urlform = '<?php echo $this->url('album\newalbum',
array('action' => 'validatepostajax'));?>';
</script>
<?php echo $this->headScript()->appendFile($this->basePath() . '/js/ajaxform-up.js'); ?>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('album\newalbum',
array('action' => 'newalbum'))
);
$form->prepare();
?>
<?php
echo $this->form()->openTag($form);
?>
<div class="element element_name">
<?php echo $this->formlabel($form->get('name')). $this->formelement($form->get('name'));?>
</div>
<div class="element element_type">
<?php echo $this->formElement($form->get('type'));?>
</div>
<?php echo $this->formElement($form->get('submit')).$this->form()->closeTag(); ?>
JS:
$(function(){
$("form#AlbumForm").submit(function(){
if (is_xmlhttprequest == 0)
return true;
$.post(urlform, { 'name' : $('input[name=name]').val(),
'type' : $('select[name=type]').val()}, function(itemJson){
var error = false;
if (itemJson.name != undefined){
$(".element_name").append("<div class = 'alert alert-error'>"+itemJson.name[0]+"</div>");
error = true;
}
if (itemJson.type != undefined){
$(".element_type").append("<div class = 'alert alert-error'>"+itemJson.type[0]+"</div>");
error = true;
}
$("winpopup").dialog("option", "position", { my: "center", at: "center", of: window });
if (!error){
$("#winpopup").dialog('close');
location.reload();
if (itemJson.success == 1){
alert('Data saved');
}
}
}, 'json');
return false;
});
});
1.你在模塊配置文件中添加了這個嗎? 'strategies'=> array( 'ViewJsonStrategy',),. 2.是否是js加載的視圖? 3.註釋一些代碼invalidatepostajaxAction()並添加一個簡單的echo和exit();看看你能否得到回覆 – dixromos98
@ dixromos98感謝您的幫助。所以:1)是我加了,雖然我在validatepostajaxAction()'$ this-> savetodb($ form-> getData())中註釋掉了;'現在只有''response => setContent(\ Zend \ Json \ Json :: encode(array('success'=> 1)));'正在執行並且警報彈出,所以我不明白爲什麼它不能保存到數據庫 – Wermerb
好吧,那麼你知道Ajax的工作原理因爲你得到的迴應(好消息)。現在您應該檢查您的本地主機日誌以查看導致問題的原因,或者測試代碼的每一行以查看導致異常的原因! – dixromos98