如何向 Laravel 5.0 框架发送 Ajax 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29217848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send an Ajax request to Laravel 5.0 framework?
提问by Charles Forest
I've tried to fix this all morning and I can't seem to find any working method online.
我整个上午都在尝试解决这个问题,但似乎在网上找不到任何工作方法。
I'm trying to do a simple ajax request to my Laravel controller and get the data it sends. I've simplified it to the maximum and I still get ERROR 500 with data "undefined".
我正在尝试向我的 Laravel 控制器发出一个简单的 ajax 请求并获取它发送的数据。我已经将它简化到最大程度,但我仍然收到数据“未定义”的 ERROR 500。
It seems that every examples onlines are about Laravel 4.0, I'm not sure if they changed something or not but none seem to work. I also tried changing the route to "any" and it works on the direct access but not with the ajax request.
似乎网上的每个例子都是关于 Laravel 4.0 的,我不确定他们是否改变了一些东西,但似乎都没有。我还尝试将路由更改为“任何”,它适用于直接访问,但不适用于 ajax 请求。
Thanks.
谢谢。
Controller :
控制器 :
<?php namespace App\Http\Controllers;
use Session, DB, Request;
class AjaxController extends Controller {
public function question()
{
print_r("Made It");
die();
}
}
Route :
路线 :
Route::post('/ajax/question', 'AjaxController@question');
Javascript :
Javascript :
$.ajax({
url: "/ajax/question",
method: 'POST',
data: { 'answered': '1' },
processData: false,
contentType: false,
cache: false,
success: function(data) {
console.log(data);
console.log("success");
},
error: function(data) {
console.log(data);
console.log("error");
}
});
console.log(data) gives the following :
console.log(data) 给出以下内容:
readyState
4
responseText
""
status
500
statusText
"Internal Server Error"
abort
function(e)
always
function()
complete
function()
done
function()
error
function()
fail
function()
getAllResponseHeaders
function()
getResponseHeader
function(e)
overrideMimeType
function(e)
pipe
function()
progress
function()
promise
function(e)
setRequestHeader
function(e, t)
state
function()
statusCode
function(e)
success
function()
then
function()
EDIT : If I change the request to GET it works properly. (Set route to any)
编辑:如果我将请求更改为 GET,它可以正常工作。(设置路由到任何)
回答by ceejayoz
If you look in your browser console at the response, chances are you're hitting the CSRF middleware. You need to post _token
with the current value for the user's csrf_token()
.
如果您在浏览器控制台中查看响应,您很可能会遇到CSRF 中间件。您需要发布_token
用户的当前值csrf_token()
。
We include this in our page layouts in order to automatically add it to all AJAX requests via a header (which Laravel understands):
我们将其包含在我们的页面布局中,以便通过标头(Laravel 可以理解)自动将其添加到所有 AJAX 请求中:
<script>
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!};,
}
});
});
</script>
See https://github.com/laravel/framework/blob/8687d42c6674e47efc093b5092ea217b62ba293a/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php#L55for details on how that works.
有关其工作原理的详细信息,请参阅https://github.com/laravel/framework/blob/8687d42c6674e47efc093b5092ea217b62ba293a/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php#L55。
回答by Hasitha Lakshan
$.ajax({
type: "POST",
URL: "test/",
data: {
id: $(this).val(), // < note use of 'this' here
access_token: $("#access_token").val()
},
success: function(result) {
alert('YES');
},
error: function(result) {
alert('Some Thing went wrong');
}
});