Reputation: 31
Here's my code: This is javascript file that sends the get the selected element from view and send that id to the controller as an Ajax request.
function getData (id, e) {
if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlHttp.onreadystatechange = function () {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert('yes');
}else{
alert('no');
}
}
xmlHttp.open('POST', '/BrandDetail/returnPdetails', true);
xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlHttp.send(id);
e.preventDefault();
}
});
Now here's the controller that is not either getting the request or responding it.
class BrandDetailController extends AppController {
public $name = 'BrandDetail';
function returnPdetails () {
$id = $_POST['id'];
$data = $this->brandDetail->find('all', array('conditions' => array('brandDetail.id' => $id)));
}
}
Upvotes: 3
Views: 445
Reputation: 4517
You should be calling the controller from your ajax submit not the model. Pluralize your path to
xmlHttp.open('POST', '/BrandDetails/returnPdetails', true);
The parameters being passed in the send function should be keys and values
xmlHttp.send("id="+id);
You are not actually sending any data or strings out to the view for your ajax response. In your controller, at least send a test string to the view before trying to manipulate data that you are unsure is even making it to the controller.
function returnPdetails(){
$id = $this->params['named']['id'];
$data = $this->BrandDetail->findById($id);
pr('Some text to test if the request is working'); }
The readystatechange function shouldn't be capitalized according to here
xmlhttp.onreadystatechange
Narrow down your issues by catching the errors in your xmlhttp.onreadystatechange function. Put in statements if the readyState is 0, 1, 2, or 3 as well. This will help you debug.
Upvotes: 1