weixin_33717298 2017-05-13 07:39 采纳率: 0%
浏览 14

Laravel通过Ajax注册

I'm trying to make user registration process via ajax in laravel. But I'm unable to do that.

Routes

Route::get('/register', 'Auth\AuthController@getRegister')->name('register');
Route::post('/register', 'Auth\AuthController@postRegister')->name('postRegister');

HTML form

<form action="{{ route('postRegister') }}" method="POST" id="registerForm">
    <h4>REGISTER NOW</h4>
    <hr><br>
    <input type="text" name="name" class="form-control" placeholder="Name">
    <br>
    <input type="number" name="student_id" class="form-control" placeholder="Student ID">
    <br>
    <input type="email" name="email" class="form-control" placeholder="Email address">
    <br>
    <input type="number" name="phone" class="form-control" placeholder="Phone no">
    <br>
    <input type="password" name="password" id="password" class="form-control" placeholder="Choose password">
    <br>
    <input type="password" name="password_confirmation" class="form-control" placeholder="Confirm password">
    <br>
    <div class="row">
        <div class="col-md-12 text-right">
            <button type="submit" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" id="registerButton"><span id="regLoader" style="display: none"><i class="fa fa-spinner fa-pulse"></i><span class="sr-only">Loading...</span>&nbsp;</span>
            Register</button>
        </div>
    </div>
    {{ csrf_field() }}
</form>

JS

$('#registerForm').submit(function(e){
    e.preventDefault();

    $.ajax({
        type: 'POST',
        url: '/register',
        data: $(this).serialize(),
        dataType: 'json',

        success: function(data){

        },
        error: function(data){

        }
    });
});
  • 写回答

3条回答 默认 最新

  • weixin_33691700 2017-05-13 09:02
    关注

    Your code looks perfect.

    1) Maybe some form validation goes failed and which prevents data to be stored in the database.

    Comment out your server side validations(if you have) and check again.

    2) If this didn't work, comment out dataType: 'json' in AJAX and check again.

    Also, print console.log() in your success method of ajax to troubleshoot.

    If none of it work, show your controller code.

    Thanks

    评论

报告相同问题?