Answer the question
In order to leave comments, you need to log in
How to pass GET parameters in Ajax request in Kohana?
I have a problem with Ajax while working with the Kohana framework. How to send parameters from browser to Kohana actions?
It is necessary to create an application where the parameters from JQuery will be sent by Ajax-som and the Kohana action.
There is an action - action_timers_list_ajax() it should receive two parameters from Jquery and return JSON code to create pagination on the page. Parameters: lines - number of records per page and pages - number of pages.
The main problem occurs on the side of the controller in the action, it does not receive variables from the GET request. I have tried using the $this->request->query('lines'); but it doesn't work. What methods should be used to pass GET parameters to the controller using Ajax ?
An example of my jQuery code:
$.ajax({
type:"GET",
url: "timerslist",
data: {"lines": "555", "pages": "1"}
contentType: 'application/json',
dataType: "json"
}).done(function(response){
console.log(response);
}).fail(function(xhr, textStatus, error){
console.log(xhr);
});
Route::set('timerslist', 'timerslist(<controller>(/<action>))')
->defaults(array(
'directory' => 'index',
'controller' => 'timer',
'action' => 'timers_list_ajax',
));
// Ajax action (rout - timerslist)
public function action_timers_list_ajax(){
$lines = (int) $this->request->query('lines');
$pages = (int) $this->request->query('pages');
$data = array(
'lines' => $lines,
'pages' => $pages
);
var_dump($lines);
exit;
if (Request::initial()->is_ajax()){
header('Content-Type: text/json; charset=utf-8');
json_encode($data);
exit;
}
}
// Ajax action
public function action_timers_list_ajax(){
// GET request
$lines = (int) $this->request->query('lines');
$pages = (int) $this->request->query('pages');
$data = array(
'lines' => $lines,
'pages' => $pages
);
var_dump($data);
exit;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question