【问题标题】:applying ajax in laravel在 laravel 中应用 ajax
【发布时间】:2015-09-21 03:15:54
【问题描述】:

我想在 laravel 4.2 中应用 ajax。我想重定向到一个页面而不重新加载它。我尝试过使用 window.loaction.hrefwindow.loaction.assign 但它不起作用。这是我的文件

Route.php

        //login+logout+auth
    Route::get('login','authenticationController@create')->before('guest');
    Route::get('logout','authenticationController@destroy');
    Route::get('create_user_view',array('as'=>'create_user','uses'=>'authenticationController@create_user'));
    Route::post('create_user_store',array('as'=>'store_user','uses'=>'authenticationController@store_user'));

Route::resource('authentication','authenticationController');

controller.php

public function store()
{
    // return Response::json(array('msg'=>'true'),200);exit;    
    $input=Input::all();
    $user=User::where('email',$input['email'])->pluck('id');

    $user_priviledge=Priviledge::where('user_id', $user)->pluck('user_authority');
    //var_dump($user_priviledge);die;

    if($user_priviledge==1)
    {   
        $attempt=Auth::attempt([
        'email'=>$input['email'],
        'password'=>$input['password']

    ]);
        //var_dump($input['password']);die;
        //var_dump($attempt);exit;
        if($attempt)
        {   

            return 'true';
        }else{
            return Redirect::intended('login');
            }
    }else{
        return Redirect::intended('login')->with('message', 'User Disabled');;
    }
}

秃头

    @extends('app')

@section('content')
<!--login form-->
<div class="login">
    {{Form::open(array('url'=>route('authentication.store')))}}
    <div>
        <h2>Sign in to your account</h2>
        <table>
            <tr>
                <th class="table">{{ Form::text('email', Input::old('E-mail'),  array('id'=>'email','placeholder'=>'E-Mail','class'=>'placeholder')) }}</th>
                <th>{{$errors->first('email')}}</th>
            </tr>
            <tr>
                <th class="table">{{ Form::password('password', array('id'=>'password','placeholder'=>'Your Password','class'=>'placeholder')) }}</th>
                <th class="table">{{$errors->first('password')}}</th>
            </tr>
            <tr>
                <div>
                    <th class="table">{{Form::button('Login',array('class' => 'btn-login','onclick'=>'myfunc()'))}}</th><!--login button code-->
                </div>

            </tr>
            <tr>
                <th class="table"><!--reset button code-->
                    <div class="reset">
                        <a href="">I can't access my account</a>
                    </div>
                </th>
            </tr>
            <tr>
                <th class="table"><!--create button code-->
                    <div class="create-account">
                        <a href="{{route('create_user')}}">Create Account</a>
                    </div>
                </th>
            </tr>
        </table>
    </div>  
</div>
<script>
    function myfunc(){

        var email =document.getElementById("email").value;
        var password =document.getElementById("password").value;

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //alert("hello");
                // var data = xmlhttp.responseText;

                // console.log(xmlhttp.respons?eText);
                var text = Json.parse(xmlhttp.responseText);
                console.log(text);
                if(xmlhttp.responseText =='true'){

                    window.location.assign('http://localhost/votting-system/public/home');
                    //alert("hello");
                }
            }
        }
        xmlhttp.open("POST",'http://localhost/votting-system/public/authentication',true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("email=" +email+ "&password=" +password);
    }
</script>
@stop

现在的问题是,当我单击提交按钮时,它应该重定向到主页存在的页面,但是当我单击登录按钮时,它什么也不做,然后当我重新加载页面时,它显示主页。这意味着它正在登录,但页面没有重新加载就不会被重定向到主页。

【问题讨论】:

    标签: javascript php ajax laravel blade


    【解决方案1】:

    你会想试试location对象的href属性

    window.location.href='http://localhost/votting-system/public/home';
    

    【讨论】:

    • 对不起,我也试过了,但它不工作。就像我点击登录按钮没有发生任何事情然后我只是重新加载它显示主页的页面。@identity Unknown
    • 您检查控制台是否有任何错误?这可能是一个 JS 错误
    • 没有错误...但是我使用正确的方式对吗?它应该工作对吗? @身份未知
    • 现在等待它的工作,但它正在重新加载页面..有没有办法在不重新加载页面的情况下这样做?@Identity Unknown
    • 对不起...你明白了,你说的重新加载到底是什么意思?
    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 2020-06-11
    • 2014-09-09
    • 2018-04-05
    • 2017-03-19
    • 2016-05-19
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多