【问题标题】:Larave Vue post request return 500 internal server errorLaravel Vue 发布请求返回 500 内部服务器错误
【发布时间】:2021-01-14 04:54:31
【问题描述】:

我刚刚进入 laravel 并且对 vuejs 没有任何了解,或者我有一个非常简单的代码试图向服务器发送请求,服务器应该返回一个带有用户名的警报这是我的代码。

blade.php

<button user-id="{{ $user->id }}"></button>

vue 组件

<template>
 <div>
   <a href="#" class="btn btn-primary" onclick="action">Follow</a>
 </div>
</template>

<script>
  export default{
    props: [userId],
    mounted(){
      console.log('Component mounted.')
    },
    methods:{
      action(){
        axious.post('/follow/' + this.userId)
          .then( response => {
            alter( response.data )
          })
      } 
    }
  }
</script>

php代码

路线

Route::post('/follow/{user}', [ProfileController::class, 'store'];

配置文件控制器类

public function store(User $user){
  return $user->username;
}

错误

app.js:285 POST http://localhost:8000/follow/2 500 (Internal Server Error)
dispatchXhrRequest @ app.js:285
xhrAdapter @ app.js:119
dispatchRequest @ app.js:765
Promise.then (async)
request @ app.js:542
Axios.<computed> @ app.js:567
wrap @ app.js:1131
followUser @ app.js:1924
invokeWithErrorHandling @ app.js:39512
invoker @ app.js:39837
original._wrapper @ app.js:45196
app.js:699 Uncaught (in promise) Error: Request failed with status code 500
    at createError (app.js:699)
    at settle (app.js:960)
    at XMLHttpRequest.handleLoad (app.js:168)

【问题讨论】:

  • 检查错误日志,看看为什么会得到 500
  • A 500 错误是一种通用错误消息,几乎涵盖了 PHP 脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。对于 Laravel,也可以查看 storage/logs
  • 在 storage/logs 文件夹中只有 laravel.log 文件超过 10000+ 行 cmets
  • 这些是排查 500 错误时非常重要的 cmets。您可以重命名该文件并再次尝试从一个干净的文件开始。我还建议changing your logging to daily 防止日志文件变得太大。
  • 单击网络选项卡,然后单击 XHR 选项卡。尝试查找文件并单击它。

标签: javascript php laravel api vue.js


【解决方案1】:

下次当您处理 api 请求并遇到此类错误时,只需切换到网络点击开发人员工具并跟踪您的请求,然后单击响应,您就可以找到整个异常跟踪。

【讨论】:

    【解决方案2】:

    我刚刚发现错误我没有在 ProfileController 类中导入用户类。

    感谢大家的快速回复。

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 2018-10-16
      • 2019-06-05
      • 2016-06-11
      • 1970-01-01
      • 2018-01-24
      • 2019-05-24
      • 1970-01-01
      • 2015-11-08
      相关资源
      最近更新 更多