Laravel 5.4 $.post() jquery.min.js:4 POST 500(内部服务器错误)
Laravel 5.4 $.post() jquery.min.js:4 POST 500 (Internal Server Error)
这是我第一次在laravel中使用jquerypost函数。我正在使用 laravel 5.4。我想通过 jquery 提交数据。
这是我的 blade 模板视图:
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<form class="form-horizontal" method="POST" action="{{ route('doctor.register') }}">
{{ csrf_field() }}
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
<a href="#" id="ajaxclick">On click ajax</a>
</form>
</body>
路线:
Route::post('/ajax', 'TestController@ajax')->name('test.ajax');
JS 文件:
$(document).ready(function() {
$( "#ajaxclick" ).click(function() {
var token = $("#token").val();
var identity = 5;
$.post( "/ajax", { id: identity, token: token },
function( data ) {
console.log( "ajax working");
console.log( data.id );
}, "json");
});
});
控制器:
public function ajax(Request $request)
{
$id = $request['id'];
$id = $id + 1;
return response()->json(array('success'=>true, 'id'=>$id));
}
但是当我点击标签时,它在控制台日志中显示 500(内部服务器错误)。
现在,我如何通过 post 函数传递数据?有帮助吗?
已解决::
我已经像下面这样编辑了我的 JS 文件:
$(document).ready(function() {
$( "#ajaxclick" ).click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var token = $("#token").val();
var identity = 5;
$.post( "/ajax", { id: identity, token: token },
function( data ) {
console.log( "ajax working");
console.log( data.id );
}, "json");
});
});
这是我第一次在laravel中使用jquerypost函数。我正在使用 laravel 5.4。我想通过 jquery 提交数据。
这是我的 blade 模板视图:
<head>
<meta charset="UTF-8">
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
<form class="form-horizontal" method="POST" action="{{ route('doctor.register') }}">
{{ csrf_field() }}
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
<a href="#" id="ajaxclick">On click ajax</a>
</form>
</body>
路线:
Route::post('/ajax', 'TestController@ajax')->name('test.ajax');
JS 文件:
$(document).ready(function() {
$( "#ajaxclick" ).click(function() {
var token = $("#token").val();
var identity = 5;
$.post( "/ajax", { id: identity, token: token },
function( data ) {
console.log( "ajax working");
console.log( data.id );
}, "json");
});
});
控制器:
public function ajax(Request $request)
{
$id = $request['id'];
$id = $id + 1;
return response()->json(array('success'=>true, 'id'=>$id));
}
但是当我点击标签时,它在控制台日志中显示 500(内部服务器错误)。
现在,我如何通过 post 函数传递数据?有帮助吗?
已解决:: 我已经像下面这样编辑了我的 JS 文件:
$(document).ready(function() {
$( "#ajaxclick" ).click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var token = $("#token").val();
var identity = 5;
$.post( "/ajax", { id: identity, token: token },
function( data ) {
console.log( "ajax working");
console.log( data.id );
}, "json");
});
});