在 zend 框架 2 中使用 ajax 不能 post 值
cant post value using ajax in zend framework 2
我正在使用 ajax 到 post 一个值,如下所示,
但是我 post 的数据不会到达控制器 ajaxAaction
查看脚本(这是一个tpl文件)
<input type="text" id='taska'>
<button id='submitTo'>button</button>
脚本
$(document).ready(
function(){
//controller via ajax
$("#submitTo").click(function() {
var message = $('#taska').val();
if (message != '') {
//run ajax
//alert ('not empty');
$.post('index/ajax',
{'message' : message},
//callback function
function (respond) {
//put respond in class show-msg
$(".show-msg").html(respond);
}
);
}
});
和行动
public function ajaxAction() {
//get post request (standart approach)
$request = $this->getRequest()->getPost();
//referring to the index
//gets value from ajax request
$message = $request['message'];
// makes disable renderer
$this->_helper->viewRenderer->setNoRender();
//makes disable layout
$this->_helper->getHelper('layout')->disableLayout();
//return callback message to the function javascript
echo $message;
}
} );
$.post('index/ajax',
是我认为错误的部分。
我是否以错误的方式定义了控制器和动作?
我被困在这里有一段时间了。
请帮忙
在 <?php echo $this->basePath();?>index/ajax
中与 $this->basePath()
一起尝试
通过在 rootPath
前面加上前缀
解决了这个问题
if (message != '') {
//run ajax rootPath
{/literal} $.post("{eval var=$rootPath}/index/ajax", {literal}
{'message' : message},
//callback function
function (respond) {
我正在使用 ajax 到 post 一个值,如下所示,
但是我 post 的数据不会到达控制器 ajaxAaction
查看脚本(这是一个tpl文件)
<input type="text" id='taska'>
<button id='submitTo'>button</button>
脚本
$(document).ready(
function(){
//controller via ajax
$("#submitTo").click(function() {
var message = $('#taska').val();
if (message != '') {
//run ajax
//alert ('not empty');
$.post('index/ajax',
{'message' : message},
//callback function
function (respond) {
//put respond in class show-msg
$(".show-msg").html(respond);
}
);
}
});
和行动
public function ajaxAction() {
//get post request (standart approach)
$request = $this->getRequest()->getPost();
//referring to the index
//gets value from ajax request
$message = $request['message'];
// makes disable renderer
$this->_helper->viewRenderer->setNoRender();
//makes disable layout
$this->_helper->getHelper('layout')->disableLayout();
//return callback message to the function javascript
echo $message;
}
} );
$.post('index/ajax',
是我认为错误的部分。
我是否以错误的方式定义了控制器和动作? 我被困在这里有一段时间了。 请帮忙
在 <?php echo $this->basePath();?>index/ajax
$this->basePath()
一起尝试
通过在 rootPath
前面加上前缀
if (message != '') {
//run ajax rootPath
{/literal} $.post("{eval var=$rootPath}/index/ajax", {literal}
{'message' : message},
//callback function
function (respond) {