YII2 读取 post 数据时出现 500(内部服务器错误)

500 (Internal Server Error) on YII2 when reading post data

我想使用 ajax 通过 post 请求向 table 插入数据,但我无法读取刚刚使用 ajax 发送的数据,这里是我的代码:

Ajax代码:

$('#newList').on('beforeSubmit', function(e){
e.preventDefault();
$("#newList").modal('hide');
$("#title").val("");
load(1);
$.ajax({
    url: '/site/add-list',
    type: 'post',
    data: {title : $("#title").val()},
    success: function(data) {
      console.log(data); // return 500 (Internal Server Error)
    }
});

});

控制器:

public function actionAddList()
{
    $req = Yii::$app->request;
    $newList = new Boards();
    return $req->post(); // return 500 (Internal Server Error)

}

每次我发送 post 请求时,它都会在控制台上显示 500(内部服务器错误)。但是当我 return 一个字符串时。 “asdasd”有效。我该如何解决这个问题?

抱歉回复晚了,所以问题出在我的控制器上,它应该看起来像这样 ajax 才能读取我返回的值

public function actionAddList()
{
    $req = Yii::$app->request;

    if ($req->isAjax) {
        $res = ['data' => $req->post()];
        $newList = new Lists();
        $newList->title = $res["data"]["title"];
        $newList->save();
        echo $res["data"]["title"]; // Isnt error anymore
    } else {
        return false;
    }
    
}

我收到错误是因为我在我的控制器上返回了一个数组,所以我只需要做这样的事情

    // Bad
    return array(
        "data" => 'blabla'
    );

    // Good
    $dat = array(
        "data" => "blabla"
    );
    return $dat["data"];

我从控制台收到 500 错误的原因可能来自 YII,因为响应内容不能是数组。

if (is_array($this->content)) {
        throw new InvalidArgumentException('Response content must not be an array.');
    } elseif (is_object($this->content)) {
        if (method_exists($this->content, '__toString')) {
            $this->content = $this->content->__toString();
        } else {
            throw new InvalidArgumentException('Response content must be a string or an object implementing __toString().');
        }
    }