0

I keep getting an internal Server Error 500 (TokenMismatchException) when doing an update from angularjs.

angular function:

  vm.eventSaved = function(event) { 

       $http.put('/api/events/' + event.eventid, vm.event).success(function(eventsuccess){
       }).error(function(err){             });

  };

Values passed through vm.event

{
    "events": 
    {
        "_token": "xGD7kTETgmBytf8exwIUYHYEC6lKcPek9NCuz6Xh",
        "eventid": "37",
        "title": "Events",
        "type": "info",
        "startsAt": "2015-08-15T10:46:00+00:00",
        "endsAt": "2015-08-15T11:46:00+00:00",
        "draggable": "true",
        "resizable": "true"
    }
}     

Laravel controller function:

public function update($id, AppointmentsRequest $request)
{       
    $appointments = Appointment::findOrFail($id);
    $appointments->update($request->all());

    return Response::json(array('success'=>true));
}

Is there something I should add to my Laravel controller to handle this? I do practically the same thing with a "delete" and I don't get this error.

user3489502
  • 3,451
  • 9
  • 38
  • 66
  • Hmm, they say they should add a CSRF header when performing ajax calls, have you tried adding the the ```X-CSRF-TOKEN``` header like described in [the documentation](http://laravel.com/docs/master/routing#csrf-x-csrf-token)? – Avalanche Aug 19 '15 at 18:01
  • With the Httprequester tool in FF, I see that a cookie is set ( XSRF-TOKEN=....) when I do a GET , but when I do a PUT I don't see if the cookie is gone – user3489502 Aug 19 '15 at 18:17
  • Ok, I think using ,{withCredentials: true} in my $http.put will help – user3489502 Aug 19 '15 at 18:30
  • I am not sure how reliable this would be. Check out [the answer I just found here](http://stackoverflow.com/a/18338635/1125161). It says that you need to add a constant to your angular app, but then again, the next answer reads from the documentation that if you are using Laravel 5, you shouldn't have any worries with all this – Avalanche Aug 19 '15 at 18:38
  • and it is in fact working fine with Laravel 5.. the problem was .. me. I wasn't looking at the right place for the error. Sorry guys, thank for your help – user3489502 Aug 19 '15 at 19:08

1 Answers1

0

In laravel 5 the crsf token cookie is set automatically when using front end frameworks such as Angular. You should consider removing "_token": "xGD7kTETgmBytf8exwIUYHYEC6lKcPek9NCuz6Xh" from the input been sent to the controller. Then try it out.

Mwaa Joseph
  • 763
  • 7
  • 12
  • Thanks. I was using the Httprequester tool in FF (for simulating a PUT) and thus, I was not able to reproduce the problem (because no token existed), so Laravel gave me the Internal Server Error 500 (TokenMismatchException). I noticed that when looking at the Laravel logs and seeing that my "real" error was not related to the token. – user3489502 Aug 20 '15 at 13:17