简体   繁体   English

在Vue.js中使用Axios将数据发布到laravel项目中时,内部服务器错误500

[英]Internal server error 500 when postig data to laravel project using Axios in Vue.js

I am trying to send data to my laravel application using axios in Vue but i had this error 我试图在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 code: 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: 路线:

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

Controller: 控制器:

    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]);
}

Please help me 请帮我

Error 500 often means something in the back-end went wrong, so you can check go to the console of your browser > network and check there the error that laravel threw, without that info we can only guess. 错误500通常表示后端出现了问题,因此您可以检查浏览器的控制台>网络并检查laravel抛出的错误,而没有我们只能猜测的信息。 so let's guess 所以让我们猜测

Since we don't know what laravel version you're using i will asume you're using 5.x 由于我们不知道您使用的是哪个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;

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

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