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 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑