i have following code
我有以下代码
In controller
在控制器中
class AccountController extends Controller
{
public function login(Request $request) {
$data=$request->all();
//print_r($request->all())
//return view('main',['data'=>$data]);
}
}
In route
在途中
Route::get('account/login', function() {
return View::make('manager.test');
});
Route::post('account/login', 'AccountController@login');
login view
登录视图
@extends('layout.manager')
@section('content')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Example of Bootstrap 3 Vertical Form Layout</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style type="text/css">
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.send-btn').click(function(){
$.ajax({
url: 'login',
type: "post",
data: {'email':$('input[name=email]').val(), '_token': $('input[name=_token]').val()},
success: function(data){
console.log(data);
}
});
});
});
</script>
</head>
<body>
<div class="bs-example">
<div class="secure">Secure Login form</div>
{!! Form::open(array('url'=>'account/login','method'=>'POST', 'id'=>'myform')) !!}
<div class="control-group">
<div class="controls">
{!! Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Email')) !!}
</div>
</div>
<div class="control-group">
<div class="controls">
{!! Form::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) !!}
</div>
</div>
{!! Form::button('Login', array('class'=>'send-btn')) !!}
{!! Form::close() !!}
</body>
</html>
@stop
when i click submit then all data from ajax will print in my browser console.i have tried to pass ajax value to view in my controller but every time it return 500 internal error.Can anyone tell how to pass ajax value to another view and where i am doing wrong?. thank you
当我点击提交然后所有来自ajax的数据将在我的浏览器控制台中打印。我试图传递ajax值来查看我的控制器,但每次它返回500内部错误。任何人都可以告诉如何将ajax值传递给另一个视图和哪里我做错了?谢谢
Update
更新
following is not working
以下是行不通的
public function login(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email)
return view('main',['data'=>$send]);
}
public function login(Request $request) {
return Redirect::to('home');
}
every think working fine .only problem when i return to view.it throw error in my browser console as 500 internal error otherwise if i print _r inside the controller then it will show data in my browser console.
每当我回到view.itly问题,我的浏览器控制台抛出错误为500内部错误,否则如果我在控制器内打印_r然后它将在我的浏览器控制台中显示数据。
update 2
更新2
In my controller
在我的控制器中
public function login(Request $request) {
$data=$request->all();
return $data;
}
Object { email: "manager@gmail.com", _token: "gqqXvVfwbKRHQWczYNfFMmB9ZB943CLfLdG…" }
3 个解决方案
#1
2
In your success callback of your jquery you can access each element of your data object as shown the demo
在成功回调jquery时,您可以访问数据对象的每个元素,如演示所示
success: function(data){
alert(data.email);
alert(data._token);
}
#2
1
I don't know what you are doing with your ajax. But I do it like
我不知道你在用ajax做什么。但我喜欢这样做
$("#myform").submit(function(e) {
e.preventDefault();
var form_url = $( this ).attr('action');
var form_data= $( this ).serialize();
$.ajax({
url: form_url,
type: 'POST',
data: form_data,
dataType: 'json',
success: function( result ){
$('#result-area').html( result );
}
});
});
And I get the appropriate response from the form.
我从表格中得到了适当的回应。
And yeah as per your update, if you want that those values should be available in Javascript, you should use laracasts/utilities
package by Jeffery Way. It's amazing to do that. You could simply do:
是的,根据您的更新,如果您希望这些值应该在Javascript中可用,您应该使用Jeffery Way的laracasts / utilities软件包。这样做真是太棒了。你可以这样做:
public function login(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email);
Javascript::put([ 'send' => $send ]);
return view('main');
}
And then in your blade you can access by simply saying
然后在您的刀片中,您只需说出即可访问
console.log('send');
And it will do the magic.
它会做出魔力。
#3
1
I think it's the route's problems.
我认为这是路线的问题。
Route:
路线:
Route::get('account/login', function() {
return View::make('manager.test');
});
Route::post('account/login', 'AccountController@login');
Change that into:
将其改为:
Route::get('account/login, ['as' => 'getLogin, 'uses' => 'AccountController@getLogin']);
Route::post('account/postLogin, ['as' => 'postLogin', 'uses' => 'AccountController@postLogin']);
Controller:
控制器:
This:
这个:
public function login(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email)
return view('main',['data'=>$send]);
}
public function login(Request $request) {
return Redirect::to('home');
}
To:
至:
public function postLogin(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email)
return $send;
}
public function getLogin(Request $request) {
return Redirect::to('home');
}
#1
2
In your success callback of your jquery you can access each element of your data object as shown the demo
在成功回调jquery时,您可以访问数据对象的每个元素,如演示所示
success: function(data){
alert(data.email);
alert(data._token);
}
#2
1
I don't know what you are doing with your ajax. But I do it like
我不知道你在用ajax做什么。但我喜欢这样做
$("#myform").submit(function(e) {
e.preventDefault();
var form_url = $( this ).attr('action');
var form_data= $( this ).serialize();
$.ajax({
url: form_url,
type: 'POST',
data: form_data,
dataType: 'json',
success: function( result ){
$('#result-area').html( result );
}
});
});
And I get the appropriate response from the form.
我从表格中得到了适当的回应。
And yeah as per your update, if you want that those values should be available in Javascript, you should use laracasts/utilities
package by Jeffery Way. It's amazing to do that. You could simply do:
是的,根据您的更新,如果您希望这些值应该在Javascript中可用,您应该使用Jeffery Way的laracasts / utilities软件包。这样做真是太棒了。你可以这样做:
public function login(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email);
Javascript::put([ 'send' => $send ]);
return view('main');
}
And then in your blade you can access by simply saying
然后在您的刀片中,您只需说出即可访问
console.log('send');
And it will do the magic.
它会做出魔力。
#3
1
I think it's the route's problems.
我认为这是路线的问题。
Route:
路线:
Route::get('account/login', function() {
return View::make('manager.test');
});
Route::post('account/login', 'AccountController@login');
Change that into:
将其改为:
Route::get('account/login, ['as' => 'getLogin, 'uses' => 'AccountController@getLogin']);
Route::post('account/postLogin, ['as' => 'postLogin', 'uses' => 'AccountController@postLogin']);
Controller:
控制器:
This:
这个:
public function login(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email)
return view('main',['data'=>$send]);
}
public function login(Request $request) {
return Redirect::to('home');
}
To:
至:
public function postLogin(Request $request) {
$data=$request->all();
$send= User::where('email','=', $data->email)
return $send;
}
public function getLogin(Request $request) {
return Redirect::to('home');
}