jQuery POST 到 CakePHP $this->data
我正在尝试使用 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 获取的,如果这在本例中很重要的话。
I'm trying to use jQuerys post-function to post a form to a CakePHP-script.
Like this:
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
}
So $_POST
isn't empty but $this->data
is... how come??
The form element i which to post data from is got from aja, if that is something that matters in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$this->data 期望您的变量名称采用以下形式
对于您的示例,将 dataString 更改为
data['ModelName']['test']=test data
它应该可以工作。
$this->data expects your variable names to be in the form
For your example change dataString to
data['ModelName']['test']=test data
and it should work.
控制器的 data 属性 可能仅在以下情况下预填充: POST 数据符合现有模型属性,例如通过表单助手 POST 数据时。因此,您可能必须发送“Test.something=testdata”,以便您可以在控制器中访问 $this->data["Test"]["something"] 。
It could be that the controller's data attribute is only prefilled when the POST data conforms to an existing model attribute, like when data is POSTed via the form helper. So you might have to send "Test.something=testdata", so that you can access $this->data["Test"]["something"] in the controller.