在 Zend Framework 2 中使用 AJAX
Use AJAX with Zend Framework 2
我想在我的 ZF2 项目中使用 AJAX 和 Jquery。确切地说,我想在超过 select 值更改时更改 select 选项。
为此,我尝试实现 AJAX 方法。我没有在互联网上找到解决我的问题的好教程,但我尝试使用不同的来源来解决这个问题。这是我的代码:
控制器:
public function init()
{
parent::init();
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch ->addActionContext('getcurrenttime', 'json')->initContext();
}
public function getcurrenttimeAction()
{
$date = new Zend_Date();
$chaine = $date->toString();
$this->_helper->json($chaine);
}
这是我的路线配置:
'administratif_personnel' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/administratif/personnel',
'defaults' => array(
'controller' => 'Administratif\Controller\Personnel',
'action' => 'liste',
),
),
'may_terminate' => true,
'child_routes' => array(
[...]
'getcurrenttime' => array(
'type' => 'Literal',
'options' => array(
'route' => '/getcurrenttime',
'defaults' => array(
'controller' => 'Administratif\Controller\Personnel',
'action' => 'getcurrenttime',
),
),
),
),
),
这是我在视图中的 Jquery 代码(取决于同一个控制器)我尝试使用两种不同的方法:
<script type="text/javascript">
/*
* Initialisation de Jquery
*/
$().ready(function() {
getCurrentTime();
});
function getCurrentTime() {
$.post("/administratif/personnel/getcurrenttime",{"format":"json"}, function(resp){
alert("test");
alert(resp);
},"json");
$.ajax({
url: "/administratif/personnel/getcurrenttime",
}).done(function(t) {
console.debug(t);
console.debug("test");
});
}
第一种方法 $.post
不起作用。我没有 return。但是 $.ajax
return 我一个 html 页面。 console.debug(t)
发给我 <html><head>....</head></html>
。我网站的一个页面。
你有什么想法吗?我认为这是路由的问题?
谢谢!
如果我正确理解了你的问题,你需要根据另一个值的变化更新一个 select 值。因此,只需将 onchange 函数绑定到发生变化的 select,获取 selected 值并通过 AJAX 将其发送到您的控制器操作,以便您可以获得新值并更新第二个 select.
她的解决方案可以在 ZF2 中使用:
JS:
$.ajax({
type : 'POST',
url : '/administratif/personnel/getcurrenttime',
data : {value: changedValue},
dataType: 'json',
success: function(data){
//success function
//current date is in data.currentdate
},
error: function(jqXHR,textStatus,errorThrown){
var error = $.parseJSON(jqXHR.responseText);
var content = error.content;
console.log(content.message);
if(content.display_exceptions)
console.log(content.exception.xdebug_message);
},
}) ;
控制器:
public function getcurrenttimeAction()
{
//get the dependent value sent via ajax
$value = $this->getRequest()->getPost('value');
// your logic here....
$date = date('Y-m-d'); // get the current date
$data = new JsonModel(array(
'currentdate' => $date,
));
return $data;
}
您还需要在 module.config.php
中启用 ViewJsonStrategy
:
'view_manager' => array(
//.............
'strategies' => array(
'ViewJsonStrategy',
),
),
我想在我的 ZF2 项目中使用 AJAX 和 Jquery。确切地说,我想在超过 select 值更改时更改 select 选项。
为此,我尝试实现 AJAX 方法。我没有在互联网上找到解决我的问题的好教程,但我尝试使用不同的来源来解决这个问题。这是我的代码:
控制器:
public function init()
{
parent::init();
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch ->addActionContext('getcurrenttime', 'json')->initContext();
}
public function getcurrenttimeAction()
{
$date = new Zend_Date();
$chaine = $date->toString();
$this->_helper->json($chaine);
}
这是我的路线配置:
'administratif_personnel' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/administratif/personnel',
'defaults' => array(
'controller' => 'Administratif\Controller\Personnel',
'action' => 'liste',
),
),
'may_terminate' => true,
'child_routes' => array(
[...]
'getcurrenttime' => array(
'type' => 'Literal',
'options' => array(
'route' => '/getcurrenttime',
'defaults' => array(
'controller' => 'Administratif\Controller\Personnel',
'action' => 'getcurrenttime',
),
),
),
),
),
这是我在视图中的 Jquery 代码(取决于同一个控制器)我尝试使用两种不同的方法:
<script type="text/javascript">
/*
* Initialisation de Jquery
*/
$().ready(function() {
getCurrentTime();
});
function getCurrentTime() {
$.post("/administratif/personnel/getcurrenttime",{"format":"json"}, function(resp){
alert("test");
alert(resp);
},"json");
$.ajax({
url: "/administratif/personnel/getcurrenttime",
}).done(function(t) {
console.debug(t);
console.debug("test");
});
}
第一种方法 $.post
不起作用。我没有 return。但是 $.ajax
return 我一个 html 页面。 console.debug(t)
发给我 <html><head>....</head></html>
。我网站的一个页面。
你有什么想法吗?我认为这是路由的问题?
谢谢!
如果我正确理解了你的问题,你需要根据另一个值的变化更新一个 select 值。因此,只需将 onchange 函数绑定到发生变化的 select,获取 selected 值并通过 AJAX 将其发送到您的控制器操作,以便您可以获得新值并更新第二个 select.
她的解决方案可以在 ZF2 中使用:
JS:
$.ajax({
type : 'POST',
url : '/administratif/personnel/getcurrenttime',
data : {value: changedValue},
dataType: 'json',
success: function(data){
//success function
//current date is in data.currentdate
},
error: function(jqXHR,textStatus,errorThrown){
var error = $.parseJSON(jqXHR.responseText);
var content = error.content;
console.log(content.message);
if(content.display_exceptions)
console.log(content.exception.xdebug_message);
},
}) ;
控制器:
public function getcurrenttimeAction()
{
//get the dependent value sent via ajax
$value = $this->getRequest()->getPost('value');
// your logic here....
$date = date('Y-m-d'); // get the current date
$data = new JsonModel(array(
'currentdate' => $date,
));
return $data;
}
您还需要在 module.config.php
中启用 ViewJsonStrategy
:
'view_manager' => array(
//.............
'strategies' => array(
'ViewJsonStrategy',
),
),