想過去的數據訪問後JSON數據yii2使用Ajax請求,但我一直得到一個500錯誤錯誤在yii2
這是Ajax請求的代碼:
<?php
$script = <<< 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->registerJS($script);
?>
當我做控制檯。日誌(鍵)這將返回
[0, 1]
這是我的控制器代碼:
if (Yii::$app->request->post()) {
echo $post = json_encode($_POST['keys']);
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
print_r($keys);
}else{
echo "1";
}
以上總是執行post請求的錯誤部分,有什麼可能是錯誤的;
添加json編碼的字符串,你想解碼。 –
請發表確切代碼 –
抱歉頭骨我想解碼$ keys的值 –