Axios请求返回错误500(Laravel,Axios和Vuejs)

时间:2020-04-25 19:38:20

标签: php laravel vue.js axios

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

"Error: Request failed with status code 500"

VueJS方法save():

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错误。 然后我检查了模型,看是否表或字段受到保护,但这对我来说很好。 我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

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

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

  1. 没有必要的“ created_at”和“ updated_at”列。
  2. “ Frage”旁边还有许多其他列,没有默认值,也需要输入。

我的解决方案:

  1. 添加缺少的两列

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