weixin_33739646 2016-09-01 06:35 采纳率: 0%
浏览 30

Ajax-Laravel 5.2身份验证

I want to send the form through ajax. I read some tutorials but i don't found a solution.

I create this CRUD with " php artisan make:auth "

Controller

protected function create(Request $request){ 
        $users = new User;
        $users->nume = $request['nume'];
        $users->prenume = $request['prenume'];
        $users->cnp = $request['cnp'];
        $users->mobil = $request['mobil'];
        $users->email = $request['email'];
        $users->password = bcrypt($request['password']);
        $users->save();
        return Response::json();   
}

Route

Route::post('/register',array(
   'as' => 'create',
    'uses' => 'Auth\AuthController@create'
));

Jquery

jQuery( document ).ready( function( $ ) {

$( '#continua1' ).on( 'submit', function() {

    //.....
    //show some spinner etc to indicate operation in progress
    //.....

    $.post(
            $( this ).prop( 'action' ),
            {
                "_token": $( this ).find( 'input[name=_token]' ).val(),
                "$users->nume": $( '#nume' ).val(),
                "$users->prenume": $( '#prenume' ).val(),
                "$users->cnp": $( '#cnp' ).val(),
                "$users->mobil": $( '#mobil' ).val(),
                "$users->email": $( '#email' ).val(),
                "$users->password": $( '#password' ).val(),

            },
            function( data ) {
                //do something with data/response returned by server
            },
            'json'
    );

    //.....
    //do anything else you might want to do
    //.....

    //prevent the form from actually submitting in browser
    return false;
});

});

FORM

<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('nume') ? ' has-error' : '' }}">
                            <label for="nume" class="col-md-4 control-label">Nume</label>

                            <div class="col-md-6">
                                <input required="required" id="nume" type="text" class="form-control" name="nume" value="{{ old('nume') }}">

                                @if ($errors->has('nume'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('nume') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('prenume') ? ' has-error' : '' }}">
                            <label for="prenume" class="col-md-4 control-label">Prenume</label>

                            <div class="col-md-6">
                                <input id="prenume" type="text" class="form-control" name="prenume" value="{{ old('prenume') }}">

                                @if ($errors->has('prenume'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('prenume') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('cnp') ? ' has-error' : '' }}">
                            <label for="cnp" class="col-md-4 control-label">CNP</label>

                            <div class="col-md-6">
                                <input id="cnp" type="text" class="form-control" name="cnp" value="{{ old('cnp') }}">

                                @if ($errors->has('cnp'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('cnp') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('mobil') ? ' has-error' : '' }}">
                            <label for="mobil" class="col-md-4 control-label">Mobil</label>

                            <div class="col-md-6">
                                <input id="mobil" type="text" class="form-control" name="mobil" value="{{ old('mobil') }}">

                                @if ($errors->has('mobil'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('mobil') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password">

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                            <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation">

                                @if ($errors->has('password_confirmation'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password_confirmation') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="button" id="continua1" class="btn btn-primary">
                                    <i class="fa fa-btn fa-user"></i> Continua
                                </button>
                            </div>
                        </div>
                    </form>
  • 写回答

2条回答 默认 最新

  • csdn产品小助手 2016-09-01 06:40
    关注

    Try this:

    JS:

    $.ajax({
        url         : '{{ url("/createuser") }}',
        method      : 'post',
        headers:
        {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        data        :
        {
            variable1   : value,   // variable and its values
        },
        success     : function(response)
        {
            // do your stuff on success
        }
    });
    

    Route:

    Route::post('/createuser',[
        'middleware'=>'auth',
        'uses'=>'UserManagementController@createUser'
    ]);
    

    Controller:

    public function createUser()
    {
        // do your stuff here
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿