views:

30

answers:

1

Hi all,

I am using cakePHP 1.26.
I was trying to use JQuery AJAX to pass some sample data to a function in a Controller,
but failed to do it.

This is the JQuery Part:

 var w="helloworld";
  var curl="http://localhost:8080/test/grab/";
  $.ajax({
  type: "POST",
  url: curl,
  data: "testing="+w,   
  success: function(data) {    
  alert(data);}
  });

And this is the Function in the Controller:

function grab(){
$g=$this->data['testing'];
return $g;
}

But the Alert msg box did not show me anything but blank message.
Please help if you could.

+2  A: 

$this->data is only filled with data in the format data[key]=value. In this case, your AJAX call's data property should look like this:

data: "data[testing]=" + w

To pass more than one, simply separate with an ampersand:

data: "data[one]=" + one + "&data[two]=" + two

Finally, you can actually nest them, like so:

data: "data[0][one]" = one[0] + "&data[0][two]=" + one[1] + "&data[1]=" + data

This will make $this->data a multi-dimensional array.

macamatic
Thank you so much for the quick help, macamatic. I have made some mistake in the code