0

Hi i'm trying to update user password using ajax but i keep getting 500 error.What i've done wrong Here is my controller

public function newPass(Request $request)
    {
        User::where('id','=', Auth::user()->id)->update(['password' => Hash::make($request->password)]);
        return response()->json(array('mess'=>'Update success'));
    }

Here is ajax

$(document).ready(function() {
    $('#btnNewPass').click(function(event) {
        event.preventDefault();
        var pass=$("#password").val();
        $.ajax({
            url: '/updatePass',
            type: 'POST',
            data: {pass: pass},
        })
        .success(function(data) {
         alert(data.mess);
     })
        .error(function() {
            alert("Error");
        });



    });
});

The routes

Route::post('/updatePass','Auth\PasswordController@newPass');

1 Answers1

0

I think you are missing the required token in your post and the TokenMismatchException is being fired.

Try putting this meta-tag in the head section:

<meta name="_token" content="{{ csrf_token() }}">

And then add this in your js file:

$.ajaxSetup({
  headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
});
subzeta
  • 419
  • 3
  • 7
  • hi @subzeta i'm already have {!! csrf_field() !!} in blade view –  Jan 24 '16 at 16:08
  • Hi there! But as far as I know you aren't using it on your ajax call. You need to provide it in all yours asynchronous post requests. You can configure it globally (setting it as a header as I explained in my answer) or providing it in all your ajax calls (see the best answer here: http://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request) – subzeta Jan 24 '16 at 16:38