简体   繁体   English

Laravel通过ajax注册

[英]Laravel registration via ajax

I'm trying to make user registration process via ajax in laravel . 我正试图通过laravel ajax进行用户registration过程。 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 HTML表单

<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 JS

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

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

        success: function(data){

        },
        error: function(data){

        }
    });
});

Your code looks perfect. 你的代码看起来很完美

1) Maybe some form validation goes failed and which prevents data to be stored in the database. 1)可能某些表单验证失败,并且会阻止数据存储在数据库中。

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. 2)如果这不起作用,请在AJAX中注释掉dataType: 'json'并再次检查。

Also, print console.log() in your success method of ajax to troubleshoot. 另外,在ajax的成功方法中打印console.log()以进行故障排除。

If none of it work, show your controller code. 如果它都不起作用,请显示您的控制器代码。

Thanks 谢谢

1) Can you able to see the xmlhttp request passing to the server through network tab of chrome/mozilla. 1)您能否通过chrome / mozilla的网络选项卡查看传递给服务器的xmlhttp请求。

 If so there is no error in your ajax/front end, 

2) Before going to route write a anonymous function in the action route to see whether the request comes till the route 2)在前往路由之前,在动作路由中写一个匿名函数,看看请求是否到达路由

Route::post('/register', function(Illuminate\Http\Request, $request) {
   dd($request);
});

If You have Auth Routes enabled then there is a possibility for the request to point RegistrationController inside Auth folder, do a dd($request) inside the constructor of that controller. 如果您启用了Auth Routes,那么请求可能会将RegistrationController指向Auth文件夹,在该控制器的构造函数中执行dd($ request)。 hence you can figure it out whether the request is coming there. 因此,您可以弄清楚请求是否即将到来。

3) Try regenerating the APP key (It is just a try if nothing works) 3)尝试重新生成APP密钥(如果没有任何作用,这只是一个尝试)

4) Try writing a new post route and call that route in ajax to post the data to the server,if that works the old route which you wrote may overridden by some other route. 4)尝试编写一个新的邮政路线并在ajax中调用该路由以将数据发布到服务器,如果这有效,则您编写的旧路由可能会被其他路由覆盖。

Try in the above order , you will somewhere catch the error. 尝试按上述顺序,你将在某处捕获错误。

I think you need to change your code like: 我认为您需要更改您的代码,如:

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

     $.ajax({
          url: $form.attr('action'),
          data: $(this).serialize(),
          dataType: 'json',

         success: function(data){

         },
          error: function(data){

         }
      });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM