Error in accessing post json data in yii2
<p>Am passing data to yii2 using ajax request but i keep on getting a 500 error</p>

<p>This is the ajax request code:</p>

<pre><code>&lt;?php 
 $script = &lt;&lt;&lt; JS

     $('form#forward_pr').on('beforeSubmit', function(e){
    var keys = $('#grid').yiiGridView('getSelectedRows');
     $.post({
       url: "forwardpr", // your controller action
       dataType: 'json',
       data: {keylist: keys},
       success: function(data) {
          alert('I did it! Processed checked rows.')
       },
     error: function(err){
       console.log("server error");
       }
    });
       return false;
    }  ) ;

    JS;
    $this-&gt;registerJS($script);
 ?&gt;
</code></pre>

<p>When i do console.log(keys) this returns</p>

<pre><code>[0, 1]
</code></pre>

<p>This is my controller code:</p>

<pre><code>if (Yii::$app-&gt;request-&gt;post()) {
         echo $post = json_encode($_POST['keys']);
          if (isset($_POST['keylist'])) {
                $keys = \yii\helpers\Json::decode($_POST['keylist']);
              print_r($keys);
            }else{
              echo "1";
          }
</code></pre>

<p>The above always executes the error part of post request, What could be wrong;</p>