简体   繁体   English

Axios 请求返回错误 500 (laravel, axios & vuejs)

[英]Axios request returns error 500 (laravel, axios & vuejs)

My axios request (combined with Laravel) gives me a 500 error in the web console when I try to save a new question (= Frage):当我尝试保存新问题(= Frage)时,我的 axios 请求(与 Laravel 结合)在 web 控制台中出现 500 错误:

"Error: Request failed with status code 500"

VueJS-method save(): VueJS 方法保存():

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

api.php: api.php:

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

FragenController.php (Controller): 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 (Model): 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"

    }

I thought maybe the route was wrong (api.php), but if I change this, I then get a 404 error, so I guess this is correct, since otherwise there would have always been a 404 error.我以为路由可能是错误的(api.php),但如果我改变它,我会得到一个 404 错误,所以我想这是正确的,因为否则总是会出现 404 错误。 Then I checked the model if maybe the table or fields were protected but this looks good to me.然后我检查了 model 是否可能表或字段受到保护,但这对我来说看起来不错。 What am I doing wrong here?我在这里做错了什么?

Thanks guys, by looking in the XHR tab, as well as in laravel.log I saw the issue:谢谢大家,通过查看 XHR 选项卡以及 laravel.log 我看到了这个问题:

I reused an older table ("Frage") that我重用了一张旧表(“Frage”)

  1. didn't have the necessary "created_at" and "updated_at" columns.没有必要的“created_at”和“updated_at”列。
  2. has lots of other columns beside "Frage" without a default value, that needed input as well.除了“Frage”之外还有很多其他没有默认值的列,也需要输入。

My solution:我的解决方案:

  1. add the missing two columns添加缺少的两列

  2. send the other column values in the this.Frage also.也发送 this.Frage 中的其他列值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM