2

This is my controller action.

public function getData(){

   if($this->request->is('ajax')) {  
      $session = $this->request->session();
      $data=$session->read('User.access_token');
      $this->set('data', $data);
      $this->set('_serialize', 'data');
   }
}

It is my controller action in cakephp 3.3. I want to getting my session data but it is showing error code 500 .I have already write session data and that is working on normal call of action. I also referred to https://stackoverflow.com/a/19861698/3110742


Below is my session write part.


public function oauthcallback(){
  $session = $this->request->session();
  $session->write('User.access_token', $google_sess);
}
Community
  • 1
  • 1
dpkrai96
  • 93
  • 1
  • 11
  • Please show the session creation part as well. – Disorder Feb 08 '17 at 10:12
  • @Rik.esh I have checked on normal request while reading session it is populated.Also I have added the way I am setting. – dpkrai96 Feb 08 '17 at 10:31
  • Whenever receiving errors, please always post **the _complete_ error**, that is, **including the _full_ stacktrace** (ideally copied from the logs where it is available in a properly readable fashion), even if the problem might be obvious to people who are familiar with CakePHP! – ndm Feb 08 '17 at 14:45
  • @ndm I am not getting any stack trace during this only a blank response is generated.Its simply skip the block where I am checking the session is exist or not condition. If I am printed some string before or after it then that is showing in response.I also want to refer to this http://stackoverflow.com/questions/11768816/php-session-variables-not-preserved-with-ajax But I think that is not a problem because when I am accessing the controller action without ajax It is showing the proper response. – dpkrai96 Feb 11 '17 at 10:24

1 Answers1

0

Need to see the ajax call to confirm but usually error 500 occurs when the wrong url is called.

I advise to use always the Url helper to avoid any issue (will also work with Routes)

Ex:

$("#my-ajax-btn").click(
    function()
    {                
        jQuery.ajax({
            type:'POST',
            async: true,
            cache: false,
            url: '<?= $this->Url->build(['plugin' => null, 'controller' => 'Controllers', 'action' => 'action']) ?>'
        });
        return false;
    }
);

CakeBook url conventions: https://book.cakephp.org/3.0/en/intro/conventions.html

CakeBook Url Helper: https://book.cakephp.org/3.0/en/views/helpers/url.html

David A.
  • 91
  • 4
  • Similler problem happend as http://stackoverflow.com/questions/11768816/php-session-variables-not-preserved-with-ajax But I think that is not a problem because when I am accessing the controller action without ajax It is showing the proper response. – dpkrai96 Feb 11 '17 at 10:25
  • With the code you posted so far is impossible to get what the problem is. Try to check the server error log, should point you into the right direction. – David A. Feb 12 '17 at 23:36
  • I am not getting any stack trace during this only a blank response is generated.Its simply skip the block where I am checking the session is exist or not condition. If I am printed some string before or after it then that is showing in response. – dpkrai96 Feb 13 '17 at 04:40
  • Did't meant the stack trace from cakephp but the [server error log](http://stackoverflow.com/questions/12834583/where-can-i-find-error-log-files) if unavailable cakephp also have an error log at `/log` where / is the cakephp application path – David A. Feb 13 '17 at 10:26
  • A bunch of thanks to you but there is no error log inside my log directory regarding this.My logs directory is full with at least 25 debug & error log file. I have cleaned it up.But the issue has been resolved I don't know how. – dpkrai96 Feb 13 '17 at 11:37
  • In side my method checking block I was also checked the session like this if($this->request->is('ajax')) { if($session->check('User.access_token')){ //... }} and the session checking block skip while I am calling this method. But the issue has been resolved and now it is not skipping the block but I don't know how.I have several changes around there.And I don't remember all.Now suddenly it is working for me. – dpkrai96 Feb 13 '17 at 11:44