jQuery POST to CakePHP $this->data

 ̄綄美尐妖づ 提交于 2019-12-22 10:36:34

问题


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.


回答1:


$this->data expects your variable names to be in the form

data[Model][Property]

For your example change dataString to data['ModelName']['test']=test data

and it should work.




回答2:


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.



来源:https://stackoverflow.com/questions/5404127/jquery-post-to-cakephp-this-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!