Selmi Med
Selmi Med

Reputation: 77

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

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:

   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

Upvotes: 0

Views: 3111

Answers (1)

Carlos Salazar
Carlos Salazar

Reputation: 1908

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. so let's guess

Since we don't know what laravel version you're using i will asume you're using 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;

Upvotes: 1

Related Questions