csrf validation in yii2 not working
<p>I have enabled csrf validation as true in my controller.But after few minutes, while submitting the form,csrf token got expired and got bad request message,eventhough I am passing csrf token through ajax.Please provide me a solution to get over this issue.</p>

<p>Below is my sample code </p>

<p>Controller</p>

<pre><code> public function beforeAction($action) 
    {
      $this-&gt;enableCsrfValidation = true;
      return parent::beforeAction($action);
    }
</code></pre>

<p>JS page</p>

<pre><code>  var csrfToken = $('meta[name="csrf-token"]').attr("content");
</code></pre>

<p>Ajax call</p>

<pre><code>  var values = { 
      'id'       : id,
     'cpcode'   : cpcode,  
     '_csrf'    : csrfToken

   };  
   $.ajax({ 
     type      : 'POST', //Method type
     url       : baseurl +'/site/test', 
     data      : values, 
    dataType  : 'json',
    success   : function(data)
      {

      }
   }
 );
</code></pre>

<p>main.php</p>

<pre><code> &lt;head&gt; &lt;?= Html::csrfMetaTags() ?&gt;&lt;/head&gt;
</code></pre>