douren5898 2015-07-03 10:07 采纳率: 100%
浏览 61

在laravel中应用ajax

I wanted to apply ajax in the laravel 4.2.i wanted to redirect to a page without reloading it.i'm have tried using the window.loaction.href and window.loaction.assign but it's not working. here are my files

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');;
    }
}

balde

    @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

Now the problem is when i clicked the submit button it should have redirected to a page where the home page exist but when i clicked the login button the it does nothing then when i reload the page it shows the home page.that means its being logged in but the page is not getting redirected to the home page without reloading.

  • 写回答

1条回答 默认 最新

  • douan9541 2015-07-03 10:14
    关注

    You would want to try href property of location object

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

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。