Jay*_*oso 8 php ajax jquery post zend-framework
我正在使用zend框架,我想使用Jquery ajax post获取POST数据,以便在不刷新页面的情况下进行保存.
//submit.js
$(function() {
$('#buttonSaveDetails').click(function (){
var details = $('textarea#details').val();
var id = $('#task_id').val();
$.ajax({
type: 'POST',
url: 'http://localhost/myproject/public/module/save',
async: false,
data: 'id=' + id + '&details=' + details,
success: function(responseText) {
//alert(responseText)
console.log(responseText);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
在我的控制器上,我只是不知道如何从ajax中检索POST数据.
public function saveAction()
{
$data = $this->_request->getPost();
echo $id = $data['id'];
echo $details = $data['details'];
//this wont work;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
kar*_*m79 14
将$.ajax's dataType选项设置为'json',并修改成功回调以从接收的JSON中读取:
$('#buttonSaveDetails').click(function (){
var details = $('textarea#details').val();
var id = $('#task_id').val();
$.ajax({
type: 'POST',
dataType: 'json',
url: 'http://localhost/myproject/public/module/save',
async: false,
// you can use an object here
data: { id: id, details: details },
success: function(json) {
console.log(json.id + ' ' + json.details);
}
});
// you might need to do this, to prevent anchors from following
// or form controls from submitting
return false;
});
Run Code Online (Sandbox Code Playgroud)
从您的控制器,发送如下数据:
$data = $this->_request->getPost();
echo Zend_Json::encode(array('id' => $data['id'], 'details' => $data['details']));
Run Code Online (Sandbox Code Playgroud)
作为结束点,请确保已禁用自动视图呈现,因此返回客户端的唯一输出是JSON对象.
| 归档时间: |
|
| 查看次数: |
16575 次 |
| 最近记录: |