gpt4 book ai didi

javascript - 在 Laravel 中应用 Ajax

转载 作者:行者123 更新时间:2023-12-03 09:40:23 24 4
gpt4 key购买 nike

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

路线.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

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

最佳答案

您想尝试 location 对象的 href 属性

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

关于javascript - 在 Laravel 中应用 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31204107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com