问题
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