首页 > 解决方案 > axios 请求返回错误 500 (laravel, axios & vuejs)

问题描述

当我尝试保存新问题(= Frage)时,我的 axios 请求(与 Laravel 结合)在 Web 控制台中出现 500 错误:

"Error: Request failed with status code 500"

VueJS 方法保存():

save: function(){
    axios.post('/api/save-frage', this.Frage) //passes the object this.Frage
        .then(res => {
            // nothing here
            });
}

api.php:

Route::post('/save-frage','FragenController@store');

FragenController.php(控制器):

 public function store(Request $request)
    {
        // validation coming soon :)
        $frage = new Frage;
        $frage->Frage = request('Fragentext');
        $frage->save(); //if I comment this out, there's no error 500 :)
    }

Frage.php(模型):

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Support\Facades\Auth;

    class Frage extends Model
    {
        protected $table = 'fragen';
        protected $fillable = ['Frage']; // only field to fill is called "Frage"

    }

我以为路由可能是错误的(api.php),但如果我改变它,我会得到一个 404 错误,所以我想这是正确的,因为否则总是会出现 404 错误。然后我检查了模型是否可能表或字段受到保护,但这对我来说看起来不错。我在这里做错了什么?

标签: phplaravelvue.jsaxios

解决方案


谢谢大家,通过查看 XHR 选项卡以及 laravel.log 我看到了这个问题:

我重用了一张旧表(“Frage”)

  1. 没有必要的“created_at”和“updated_at”列。
  2. 除了“Frage”之外还有很多其他没有默认值的列,也需要输入。

我的解决方案:

  1. 添加缺少的两列

  2. 也发送 this.Frage 中的其他列值。


推荐阅读