Laravel Ajax用户注册页面不起作用

时间:2019-04-11 02:55:14

标签: php laravel laravel-5

我正在尝试使用jQuery使用Laravel制作我的注册页面来提交我的表单,但是它不起作用。它什么也没返回,我不知道怎么了。

这是我的代码:

web.php

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

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Auth\Events\Registered;

class RegisterController extends Controller{

    use RegistersUsers;

    protected $redirectTo = '/home';

    public function __construct(){
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
         $vaild =   Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);

        if($vaild->fails()){
            return json_encode($vaild->errors());
        }

        return true;

    }

    public function register(Request $request)
    {
        $v = $this->validator($request->all());

        if(!is_bool($v)){
            return $v;
        }

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    protected function registered(Request $request, $user)
    {
        return "true";
    }


}

CreateCustomer.php

<form action="{{ route('postRegister') }}" method="POST" id="registerForm">
                                @csrf
                                <fieldset >
                                    <div class="col-md-8"> 
                                        <div class="form-group">
                                            <label >Full Name:</label>
                                            <input type="text" name="name" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <label >Phone Number:</label>
                                            <input type="text" name="phoneNumber" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <label >E-mail:</label>
                                            <input type="text" name="email" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <label >Address</label>
                                            <input type="text" name="address" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <label >Password</label>
                                            <input type="password" name="password" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <label >Repeat Password</label>
                                            <input type="password" name="password_confirmation" class="form-control">
                                        </div>
                                        <div class="form-group">
                                            <img src="{{ asset('images/load.gif') }}" style="display:none;" id="registerImg" />
                                            <h6 class="mb-3 text-danger" id="registerShow"></h6>
                                            <button type="Submit" id="registerSubmit" class="theme_btn" onclick="submit_form('registerForm', 'registerSubmit', 'registerImg', 'registerShow', 'yes')" >Create Customer</button>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>

submit.js

function submit_form(formid, btn_id, img_id, show_id, reload){
    var frm = $('#'+formid);
    frm.submit(function(e){
        e.preventDefault();
        $("#"+btn_id).hide();
        $("#"+img_id).show();
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data:  new FormData(this),
            dataType:'JSON',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            processData:false,
            success: function (data){ 
                console.log(data)
                $("#"+btn_id).show();
                $("#"+img_id).hide();
                if(data.errors){   
                    $("#"+show_id).html(''); 
                    $.each(data.errors, function(key, value){
                        $("#"+show_id).append('<p class="text-danger">'+value+'</p>');
                    });                                   
                } else{
                    $("#"+show_id).addClass('text-success').fadeIn().html(data.success); 
                    if(reload == 'yes'){
                        setTimeout(function(){
                            location.reload();
                        } , 2000);
                    }
                    else{
                        frm[0].reset();    
                        setTimeout(function(){
                            $("#"+show_id).fadeOut('slow').html(data.success);
                        } , 10000);
                    } 
                }
                $("#"+btn_id).show();             

            },
            error: function(data){       
                console.log(data)
                $("#"+btn_id).show();
                $("#"+img_id).hide();
            },
        });
    });
}

我在网上搜索,但没有发现任何有用的信息。

我如何使此表格生效?

1 个答案:

答案 0 :(得分:0)

发布数据时可能缺少csrf令牌字段。要在设置数据值后设置页眉,将其移动到数据值之前

 { headers: { 'csrftoken' : '{{ csrf_token() }}' },