Welcome Guest, Not a member yet? Register   Sign In
ajax request instancing an object controller ?
#1

(This post was last modified: 04-07-2015, 07:25 AM by casa.)

Hello,
Is a controller method call by ajax request instancing an object controller ?
Suppose that in a view we do a ajax query that calls a method of a controller other than the one allowed to load the view.
Is the act of placing this ajax request will instantiate the object controller and therefore wanted to call its default __construct () method before calling the method called by ajax request?
Example :
- controller A load the view A_b
- In view A_b, ajax request calls method search() in a controller B who has __construct() like under, is the variable named 'var' will be initiate all the time or sometimes not ? (that is to say even if the session variable exists on the server).

I ask it because in my case sometimes i have the message of end session returns (cf. infra) whereas session is not finished.
$this->session->userdata('key') is initiate at the connexion for example ($this->session->set_userdata('key' ,'value')) and then i call many times by ajax function in controller B but sometimes, $var == '' whereas session time is not unfinished.

My Controller A (example)
PHP Code:
class extends CI_Controller
{
    private 
$view1 'A_b' ;
    
    public function 
__construct() { parent::__construct() ; }
    
    public function 
load_ab()
    {
        
$this->load->view($this->view1) ;
     }


My Controller B
PHP Code:
class extends CI_Controller
{
   private 
$var ;
   public function 
__construct()
  {
      
parent::__construct();
      
$this->var $this->session->userdata('key'); 
   }

  
// function call by ajax request from view named 'A_b'
  
public function search()
  {
      
// if session ok do action. 
      
if(isset($this->var) && trim($this->var) != '')
      { 
            
// actions to do 
      
}
      else
      {
          
// if my private $var not exist, then send a message of end session in the view A_b
         
$msg = array('status' => 'ko''message' => 'end of session. please log') ;
         echo 
json_encode($msg) ;
      }
   }

Thanks for your help.
Reply
#2

Yes, an AJAX request will call the controller's constructor. However, you probably need to read through this portion of the user guide:
http://www.codeigniter.com/user_guide/li...oncurrency

It's also possible that you are having some other issue related to session handling and AJAX, in which case your best solution may be to move some of the error handling to the client by requiring multiple missed requests before assuming the session has expired (however, if the problem IS a concurrency issue, this could make it worse).
Reply
#3

Thank you.
I search about concurrency but it's possible that the problem became from the enterprise where i work because there are some micro-cuts in the network apparently. But i continue to search and review my code.
Have a good day.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB