我正在使用 ajax 来发布一个值,如下所示,
但我发布的数据不会到达 Controller 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',
是我认为错误的部分。
我是否以错误的方式定义了 Controller 和操作? 我已经被困在这里有一段时间了。 请帮忙
最佳答案
尝试使用$this->basePath()
在<?php echo $this->basePath();?>index/ajax
关于javascript - 无法在 zend 框架 2 中使用 ajax 发布值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788423/