如何在laravel中使用post方法的ajax请求
2016-09-07 14:55
423 查看
原文地址:点击打开链接
主要有两种方法局部和全局的。
If you are going to work with ajax data post to controller or route in laravel 5. There are some need to get ajax call work correctly. Your requirement is csrf token. A default feature in Laravel is it’s automatic CSRF security. When you working with forms
it’s automatically add a “_token” hidden field to your form. On each post request token will be matched for csrf protection. so it’s one more cool feature provided by laravel 5.
If you are using default way for ajax data post, you will get “500 internal server error” in laravel 5. cause you missing csrf token posting with ajax post data. and error reason is token not being matched on post request. so every time a form data is posting
require csrf token match. else you will get “500 internal server error” in laravel.
![](http://tutsnare.com/wp-content/uploads/2015/04/laravel-ajax-post.png)
In this article we will explore how to solve 500 internal server error in ajax post or call in laravel or how to Post data using ajax in laravel 5. there are many ways to do this but i am sharing two ways :-
1. Adding on each request
2. globally
In this way we need to add token on each ajax call with data which is posting
view :- add a “login.blade.php” under “resources/views/” and add below code to make a form
Now add your ajax call or post data script to your layout footer or in the same file.
Routes :- Add your get and post route to “app/Http/routes.php”
Controller :- add a controller to “app/Http/Controllers” with named “AccountController.php” and add below code
After all make go to your page url and click on button and you get your data has been posted and you will get alert with success.
In this way we will add token for globally work with ajax call or post. so no need to send it with data post.
1. Add a meta tag to your layout header :- csrf_token() will be the same as "_token" CSRF token that Laravel automatically adds in the hidden input on every form.
2. Now add below code to footer of your layout, or where it will set for globally or whole site pages. this will pass token to each ajax request.
Now make an ajax post request an you are done your data will post successfully.
主要有两种方法局部和全局的。
Post data using ajax in laravel 5 to controller
If you are going to work with ajax data post to controller or route in laravel 5. There are some need to get ajax call work correctly. Your requirement is csrf token. A default feature in Laravel is it’s automatic CSRF security. When you working with formsit’s automatically add a “_token” hidden field to your form. On each post request token will be matched for csrf protection. so it’s one more cool feature provided by laravel 5.
Why on ajax post 500 internal server error laravel :-
If you are using default way for ajax data post, you will get “500 internal server error” in laravel 5. cause you missing csrf token posting with ajax post data. and error reason is token not being matched on post request. so every time a form data is postingrequire csrf token match. else you will get “500 internal server error” in laravel.
![](http://tutsnare.com/wp-content/uploads/2015/04/laravel-ajax-post.png)
In this article we will explore how to solve 500 internal server error in ajax post or call in laravel or how to Post data using ajax in laravel 5. there are many ways to do this but i am sharing two ways :-
1. Adding on each request
2. globally
How to Post data using ajax in laravel 5 :-
1. Adding on each request and post data to controller :-
In this way we need to add token on each ajax call with data which is postingview :- add a “login.blade.php” under “resources/views/” and add below code to make a form
<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() !!}
Now add your ajax call or post data script to your layout footer or in the same file.
<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){ alert(data); } }); }); }); </script>
Routes :- Add your get and post route to “app/Http/routes.php”
Route::get('account/login', function() { return View::make('login'); }); Route::post('account/login', 'AccountController@login');
Controller :- add a controller to “app/Http/Controllers” with named “AccountController.php” and add below code
<?php namespace App\Http\Controllers; use Input; use Request; class AccountController extends Controller { public function login() { // Getting all post data if(Request::ajax()) { $data = Input::all(); print_r($data);die; } }
After all make go to your page url and click on button and you get your data has been posted and you will get alert with success.
2. globally way :-
In this way we will add token for globally work with ajax call or post. so no need to send it with data post.1. Add a meta tag to your layout header :- csrf_token() will be the same as "_token" CSRF token that Laravel automatically adds in the hidden input on every form.
<meta name="_token" content="{!! csrf_token() !!}"/>
2. Now add below code to footer of your layout, or where it will set for globally or whole site pages. this will pass token to each ajax request.
<script type="text/javascript"> $.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') } }); </script>
Now make an ajax post request an you are done your data will post successfully.
相关文章推荐
- Django框架下如何使用ajax的post方法
- 在VC中WININET如何使用HTTP的POST方法
- android HTTP post方法时,如何使用cookies
- Symfony2CookBook:如何在路由中使用GET和POST以外的HTTP方法
- springMVC正确使用GET POST PUT和DELETE方法,如何传递参数
- Django框架如何使用ajax的post方法
- Rails2.0中如何使用jQuery的post方法?
- laravel 文章发布功能----ckeditor使用(附带post处理方法)
- 使用laravel开发网站时,如何实现前端导航栏共享数据二种简单的方法
- 抓包工具Fiddler的使用教程(十一):如何知道HTTP请求是POST方法还是GET方法
- 如何使用POST 方法调用服务
- 如何优雅的使用 laravel 的 validator验证方法
- 在VC中WININET如何使用HTTP的POST方法
- 在VC中WININET如何使用HTTP的POST方法
- 在VC中WININET如何使用HTTP的POST方法
- 如何使用web api测试工具siege和ab的post方法来发送json数据
- Visual C#2005——如何使用ToArray方法返回一个强类型数组
- anjuta的基本使用方法(包括如何设置MYSQL)
- 第4代白盒测试方法实践之“内存泄露检查工具VLD如何与VcTester配合使用”
- 在.NET2.0中如何更简单的使用委托将方法加载到事件中去