[英]Laravel Ajax JSON Object is empty
I'm working on a laravel web app, I'm relatively new to laravel so I'm not very familiar with the ajax tokens and such, but I believe I have set it up correctly. 我正在使用laravel网络应用程序,对laravel来说我还比较陌生,所以我对ajax令牌等不是很熟悉,但是我相信我已经正确设置了。
Below is my Ajax code for a test page that I want to try and send data from. 下面是我要尝试从中发送数据的测试页的Ajax代码。
<meta name="csrf-token" content="{{ csrf_token() }}">
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#btn').on('click', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'testSend',
data: {
test: 'hello'
},
dataType: 'json',
success: function (response) {
alert(response);
},
error: function (data) {
console.log('Error:', data);
}
});
});
});
</script>
Here is my form: 这是我的表格:
<div class="flex-center position-ref full-height">
<div class="content">
<form action="testSend" method="post" enctype="multipart/form-data">
<button type="submit" id="btn"> submit</button>
</form>
</div>
And here is a simple route that I have setup just to test this: 这是我设置的一条简单路线,仅用于测试:
Route::post('testSend', function (Request $request) {
return response()->json($request);
});
However when I go to check the network in chrome, the JSON Object is empty. 但是,当我去检查Chrome中的网络时,JSON对象为空。
The empty JSON Object: 空的JSON对象:
I'm pretty new to this, I have learned it but never really tried to create a web app until now. 我对此很陌生,虽然我已经学到了,但直到现在才真正尝试过创建Web应用程序。 I have searched everywhere, but no one seemed to have the same problem, maybe they did but I just don't know.
我到处搜索过,但似乎没有人遇到相同的问题,也许他们找到了,但我只是不知道。
The problem might just be a dumb mistake, but I really can't figure it out ): 这个问题可能只是一个愚蠢的错误,但我真的无法弄清楚):
Thanks. 谢谢。
而不是传递Request
实例,请尝试以下操作:
return response()->json($request->all());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.