Laravel 5.2 Modal popup for forget Password displaying 500 internal server Error while running ajax function

1.2k views Asked by At

I am trying to Check weather email exist or not to send reset password link. For that am using ajax function but its showing 500 internal server error before runing ajax. I knw 500 is for token miss match error but do i need token to check email existing or not. But I think so, with the form tag token is auto generated and then also am using {{ csrf_field() }} But it showing same error 500 Internal server. Just Help how can resolve this issue and do i need to generate token manually.

Views : login.blade.php

    {!! Form::open(array('url' => '/auth/login', 'class' => 'form-   login')) !!}
    <div class="form-title"> <span class="form-title">Login</span> </div>
     <div class="form-group">
        <label class="control-label visible-ie8 visible-ie9">Username</label>
        <input class="form-control form-control-solid placeholder-no-fix required" type="text" autocomplete="off" placeholder="Email" name="email" />
      </div>

      <div class="form-group">
        <label class="control-label visible-ie8 visible-ie9">Password</label>
        <input class="form-control form-control-solid placeholder-no-fix passwordclass required" type="password" autocomplete="off" placeholder="Password" name="password" />
      </div>

    <div class="form-actions">
       <button type="submit" class="btn red btn-block uppercase clicklogin" style="background-color:#d5ed31 !important;border-radius:4px !important;border:1px solid #ddd;">Login</button>
     </div>


    <div class="form-actions">
      <div class="pull-left">
        <label class="rememberme mt-checkbox mt-checkbox-outline">
        <input type="checkbox" name="remember" value="1" />
          Remember me <span></span> </label>
      </div>

    <div class="pull-right forget-password-block"> <a class="btn-link" data-target="#modalforget" data-toggle="modal">Forgot Password? </a></div>
    </div>

    {!! Form::close() !!}

    <!-- END LOGIN FORM -->






     <!-- BEGIN FORGOT PASSWORD FORM-->
     {!! Form::open(array('url'=>'password/reset','class' => 'form-horizontal create_forget_form','method'=>'POST', 'id' =>'forgetdata', 'files' => true)) !!}

    {{ csrf_field() }}


    <div class="modal fade" id="modalforget" tabindex="-1"   data-width="760px" >
    <div class="modal-dialog" style="width:760px !important;">
    <div class="modal-content" style="background-color:#26344b;padding:20px;border-radius:12px !important;">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
          <div class="form-title"> <span class="form-title">Forget Password</span> </div>
        </div>
        <div class="modal-body" style="height:auto !important;">
          <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    <strong style="color: #ffffec; font-size: large; ">&nbsp; &nbsp; &nbsp; &nbsp; Please enter your email. A password reset link will be sent to this email.</strong>
                  </div>
                </div>
            </div>
          <div class="row"></div>
          <div class="row">
            <div class="col-md-10">
              <div class="form-group">
                <div class="col-md-2"></div>
                <div class="col-md-10">
                <input class="form-control required" type="email" autocomplete="off" placeholder="Email"  id="forget_email" name="forget_email"style="font-size: large;" />
                        <span class="my-error-class" id="email_status"></span>
                </div>
              </div>
            </div>
          </div>
      <div class="modal-footer">
          <button type="button" value="Submit" id="submitButton" class="btn green">Submit</button>
          <button type="button" value="Cancel" class="btn default red" data-dismiss="modal">Cancel</button>
          <button type="reset" value="Clear" class="btn blue">Clear</button>
      </div>
        </div>
        </div>
      </div>
  </div>
</form>  

Ajax function in login.blade.php

<script type="text/javascript">
 $('#submitButton').click(function(){
  var formdata=new FormData($(this)[0]);
    $.ajax({
        url:"{{ URL::to('password/reset') }}",
        type: 'POST',
        data: formdata,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        context:this,
        dataType: "json",
        success: function (returndata)
        {
                if(returndata == 0)
            {
                $( '#email_status' ).html('This Email does not Exists');
                return false;

            }
            else
            {
                $( '#email_status' ).html("");
                return false;
            }
        },
        error: function(data)
        {
            console.log(data);
        }
    });
});
</script>

Route file:

Route::group(['middleware' => 'auth'], function()
{ 
    Route::post('password/reset','Auth\AuthController@resetPassword');
}

Controller Auth\AuthController@resetPassword:

 public function resetPassword()
  {
    $email =Input::get('forget_email');
    $user_email = DB::table('users')->where('email', $email)->whereNull('deleted_at')->first();
    if($user_email)
    {
        return '1';
    }
    else
    {
        return '0';
    }

 }

Screen Shot Of Error

2

There are 2 answers

2
Himanshu Upadhyay On

I think you have to write your route outside auth group.

Route::post('password/reset','Auth\AuthController@resetPassword');
3
Mayank Pandeyz On

The issue is you are using POST request with ajax and for every post request you have to pass the csrf-token with the request like:

headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},

Put this in your ajax code and everything will be fine.

Or if you want to exclude the specific URI from CSRF verification, go to project/app/Http/Middleware, there is a file VerifyCsrfToken.php in which put the routes list in:

protected $except = [
   // route list here
];

All the routes defined here excludes from csrf-token check.