【问题标题】:Internal server error 500 when postig data to laravel project using Axios in Vue.js在 Vue.js 中使用 Axios 将数据发布到 laravel 项目时出现内部服务器错误 500
【发布时间】:2018-09-30 00:13:14
【问题描述】:

我正在尝试使用 Vue 中的 axios 将数据发送到我的 laravel 应用程序,但我遇到了这个错误

Error: Request failed with status code 500
    at e.exports (axios.min.js:8)
    at e.exports (axios.min.js:8)
    at XMLHttpRequest.l.(anonymous function) (http://localhost:8000/js/axios.min.js:8:3282)

Axios 代码:

   addExperience: function () {
                axios.post(window.Laravel.url + '/addexperience', this.experience)
                    .then(response => {
                        console.log(response.data)
                    if(response.data.etat){
                            this.open = false;
                            this.experiences.push(this.experience);
                }})
                .catch(error => {
                    console.log(error);
                    console.log(window.Laravel.url + '/addexperience');
                    console.log(this.experience.titre);
                    console.log(this.experience);
                });
            }
        },

路线:

    Route::post('/addexperience', 'CvController@addExperience');

控制器:

    public function addExperience(Request $request){
    $exp = new Experience();
    $exp->titre = $request->titre;
    $exp->description = $request->description;
    $exp->debut = $request->debut;
    $exp->fin = $request->fin;
    $exp->cv_id = $request->cv_id;
    $exp->save();

    return Response()->json(['etat' => true, 'id' =>$exp->id]);
}

请帮帮我

【问题讨论】:

  • 由于500 是服务器错误,请检查服务器的日志/错误。
  • 检查您的网络选项卡以查看实际错误。这可能是一百万件事
  • 您刚刚从浏览器控制台复制了消息,而不是服务器日志。检查启动 laravel 服务器的控制台。
  • 或者您可以在 .env 文件中设置 APP_DEBUG=true 以便您可以在使用 Chrome 开发者工具或 Firebug 的 AJAX 调用中看到完整的错误堆栈跟踪
  • app_debug 为 true 且 apache 错误日志文件不包含我无法找到解决方案的错误

标签: javascript php laravel vue.js axios


【解决方案1】:

错误500 通常意味着后端出现问题,因此您可以检查转到浏览器的控制台 > 网络并在那里检查 laravel 抛出的错误,没有这些信息我们只能猜测。所以让我们猜猜

由于我们不知道您使用的是什么 laravel 版本,所以我假设您使用的是 5.x

 public function addExperience(Request $request){
        $exp = new Experience(); //Maybe this model wasnt injected, do use App\Experience; to do so
        //(Generally models are added in the app directory, if yours is in a subforlder just add it to the path, use App\folder\Experience);
        $exp->titre = $request->titre;
        $exp->description = $request->description;
        $exp->debut = $request->debut;
        $exp->fin = $request->fin;
        $exp->cv_id = $request->cv_id;
        $exp->save();

        return Response()->json(['etat' => true, 'id' =>$exp->id]);
        //if youre using the response helper it should be in lowercase "response()" if you're using the facade you must import the dependency with use Illuminate\Support\Facades\Response;

【讨论】:

  • Thanx Carlos .Experience 模型已经注入。我尝试了你的所有建议,但没有结果:/
  • 您可以右键单击浏览器 > 检查元素 > 转到网络选项卡,然后再次执行 ajax 并检查出现 500 错误的网络调用,单击它并在响应选项卡中您应该看到问题是
  • 非常感谢卡洛斯。我听从了你的建议。这是一个错过的数据库列(user_id),我忘了声明它
猜你喜欢
  • 2020-12-13
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 2021-11-15
  • 2018-10-16
  • 2018-08-27
  • 2017-01-16
  • 1970-01-01
相关资源
最近更新 更多