A
A
Alexander2016-01-05 15:02:33
JavaScript
Alexander, 2016-01-05 15:02:33

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);
});


Perhaps the problem is in the settings of the bootstrap.php file and you need to change the paths? I have created a timerslist path which is accessed by jQuery and is accessed
Code example in bootstrap:
Route::set('timerslist', 'timerslist(<controller>(/<action>))')
  ->defaults(array(
                'directory'  => 'index',
                'controller' => 'timer',
                'action' => 'timers_list_ajax',		
  ));


Action code example:
// 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;    

            }          

    }


I tried to find, I changed the action_timers_list_ajax () so that it can be accessed directly from the browser and pass variables using the GET method. I typed the path in the browser: localhost/my_project/timerslist?lines=555&pages=1

Changed code

// 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;            
    }


But still, the variables do not get into the action_timers_list_ajax() method, var_dump() displays an empty string.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sl1m_dogg, 2016-01-05
@sl1m_dogg

kohana :'(

W
WebSpider, 2016-01-05
@WebSpider

The code is correct, try outputting var_dump($_GET); - if it is empty here, then the problem is in the server configuration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question