Reputation: 156
struggling hard to get response from controller on changing value of drop down but getting internal server error
$( document ).ready(function() {
//alert( "ready!" );
$('#devices-types-id').on('change', function() {
alert( this.value );
$.ajax({
type:"POST",
url: "<?php echo Router::url(array('controller'=>'FeaturesLists','action'=>'ajax_get_parent', '_ext' => 'json'));?>",
dataType: 'text',
async:false,
success: function(data){
alert(data);
},
error: function (data) {
alert(data);
}
});
});
but getting error with path first
POST http://localhost/compare_devices/features-lists/%3C?php%20echo%20Router::ur…ction%27=%3E%27ajax_get_parent%27,%20%27_ext%27%20=%3E%20%27json%27));?%3E 404 (Not Found)
if i write path in ajax call like this url: "/ajax_get_parent" then it does not give path error but "Internal server error"
POST http://localhost/compare_devices/features-lists/ajax_get_parent 500 (Internal Server Error)
Controller code
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->Auth->allow(['ajaxGetParent']);
}
public function ajaxGetParent() {
$this->layout = false;
$this->set('text', 'SUCCESS');
$this->set('_serialize', ['text']);
}
In routes.php i put this line
Router::extensions('json', 'xml');
i'm trying to get result in json but unable. Can you just guide me how i can do this. Many questions are already posted with same topic on 'SO' but most of them are for Cakephp 2.x and i tried some different solutions(help from SO and google) but could n't solve it. can you guide me how i can get response in json and resolve path problem.
Upvotes: 0
Views: 3487
Reputation: 31
Try making extensions an array:
Router::extensions(['json', 'xml']);
Here's an ajax call that works for me:
// ajax jquery testing
function doAjaxTest() {
$.ajax({
async:false,
url: '/requests/get-order-info/123123/2121.json',
success: function(data){
alert(data);
},
error: function (data) {
alert(data);
}
});
}
controller function is:
$response['result'] = "success";
$response['has_info'] = false;
$this->set(compact('response'));
$this->set('_serialize', ['response']);
You could also try to access the required URL in the browser directly. Also check the logs for the exact server error. Use dashes instead of underscores in URL.
Upvotes: 1
Reputation: 5767
If is your jQuery code on same page Change action name:
url: "<?php echo Router::url(
array(
'controller'=>'FeaturesLists',
'action'=>'ajaxGetParent', // <--------
'_ext' => 'json'
));?>",
if is your jQuery code in external .js file:
url: $('#devices-types-id').data('ajax-url);
and in your view add:
$this->Form->input('device_type_id',
['data-ajax-url' => $this->Url->build([
'controller'=>'FeaturesLists',
'action'=>'ajaxGetParent',
'_ext' => 'json'])
]);
In Router:
Router::extensions(['json']);
Controller method
public function ajaxGetParent() {
//remove or comment this line, cakephp by default use ajax.ctp layout.
#$this->layout = false;
$this->set('text', 'SUCCESS');
$this->set('_serialize', ['text']);
}
if i write path in ajax call like this url: "/ajax_get_parent" then it does not give path error but "Internal server error"
Try:
POST http://localhost/compare_devices/features-lists/ajax-get-parent.json
Upvotes: 1
Reputation: 497
set '_full' paramater to true.
url: "<?php echo Router::url(
array(
'controller'=>'FeaturesLists',
'action'=>'ajax_get_parent',
'_ext' => 'json',
'_full' => true //for full url path
));?>",
Upvotes: 0