【问题标题】:Axios request returns error 500 (laravel, axios & vuejs)axios 请求返回错误 500 (laravel, axios & vuejs)
【发布时间】:2020-08-09 09:24:54
【问题描述】:

当我尝试保存新问题(= Frage)时,我的 axios 请求(与 Laravel 结合)在 Web 控制台中出现 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 错误。 然后我检查了模型是否可能表或字段受到保护,但这对我来说看起来不错。 我在这里做错了什么?

【问题讨论】:

  • 那么,你能展示一下你在 $request 中有什么吗? dd($request-&gt;all())
  • status code 500 是一个通用的“包罗万象”响应。检查/storage/logs/laravel.log 文件以查看错误跟踪
  • 你检查过网络检查器,XHR 选项卡吗?
  • 使用 Insomnia 进行调试。它会指出错误。
  • 您可以从响应中添加您的错误吗?您可以在网络标签中找到它

标签: php laravel vue.js axios


【解决方案1】:

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

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

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

我的解决方案:

  1. 添加缺少的两列

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

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 1970-01-01
    • 2020-12-07
    • 2019-04-20
    • 2018-08-03
    • 2020-06-21
    • 2019-09-29
    • 2021-07-05
    • 2018-04-19
    相关资源
    最近更新 更多