我正在尝试使用 jQuery 后置函数将表单发布到 CakePHP 脚本。
像这样:
jQuery:
$('#submit_btn').click(function(){
//Code to prevent redirect
dataString = 'test=testdata';
$.post('cakephp/forms/output', dataString, function(response){
alert(response);
})
});
CakePHP
function output(){
pr($this->data); # Print nothing
pr($_POST); # Print test => testdata
$this->render('view','ajax'); # Render ajax-friendly
}
所以 $_POST
不是空的,但是 $this->data
是......怎么会?
用于发布数据的表单元素 i 是从 aja 获取的,如果这在本例中很重要的话。
最佳答案
$this->data 期望您的变量名称采用以下形式
data[Model][Property]
对于您的示例,将 dataString 更改为 data['ModelName']['test']=测试数据
它应该可以工作。
关于jQuery POST 到 CakePHP $this->data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5404127/