通过按 blade 视图上的按钮但不刷新页面来更新 Laravel 中调用控制器功能的用户属性
Update user attribute in Laravel calling a function on controller by pressing a button on blade view but without refresh page
我有 blade Laravel 查看我有一个按钮,单击它我想更新一些用户属性。我在控制器中实现了一种方法,该方法将要报告的值传递给用户并保存在用户身上,但我不想更新页面或更改 URL。执行此操作的正确方法是什么?
谢谢。
web.php
Route::post('/updateUserPreference/{plan_compact_view}', [NutritionalPlanController::class, 'up
dateUserPreference'])->name('updateUserPreference');
blade 查看
@push('scripts')
<script type="text/javascript">
$(document).ready(function () {
$('#btn_view_type').click(function (e) {
//AJAX Call update user preference
$.ajax({
url: 'updateUserPreference',
method: 'post',
dataType: 'JSON',
success: function () {
console.log("done");
},error: function(xhr, ajaxOptions, thrownError){
console.log("error");
}
});
});
});
</script>
@endpush
更新 2
const planCompactView = true;
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '/updateUserPreference/${planCompactView}',
method: 'post',
/* send the csrf-token and the input to the controller */
dataType: 'JSON',
data: {},
success: function () {
console.log("done");
//do something
},error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status+" ,"+" "+ajaxOptions+", "+thrownError);
}
});
现在控制台中的错误消息是:“200,parsererror,SyntaxError:JSON 输入意外结束”
需要更改 ajax 代码段中的 url 以匹配路由文件
中定义的 url
您还需要获取路由参数 plan_compact_view
的值并将其附加到 js 代码段
中的 url
@push('scripts')
<script type="text/javascript">
const planCompactView = 'get the value';
$(document).ready(function () {
$('#btn_view_type').click(function (e) {
//AJAX Call update user preference
$.ajax({
url: `/updateUserPreference/${planCompactView}`,
method: 'post',
dataType: 'JSON',
success: function () {
console.log("done");
},error: function(xhr, ajaxOptions, thrownError){
console.log("error");
}
});
});
});
</script>
@endpush
我有 blade Laravel 查看我有一个按钮,单击它我想更新一些用户属性。我在控制器中实现了一种方法,该方法将要报告的值传递给用户并保存在用户身上,但我不想更新页面或更改 URL。执行此操作的正确方法是什么?
谢谢。
web.php
Route::post('/updateUserPreference/{plan_compact_view}', [NutritionalPlanController::class, 'up
dateUserPreference'])->name('updateUserPreference');
blade 查看
@push('scripts')
<script type="text/javascript">
$(document).ready(function () {
$('#btn_view_type').click(function (e) {
//AJAX Call update user preference
$.ajax({
url: 'updateUserPreference',
method: 'post',
dataType: 'JSON',
success: function () {
console.log("done");
},error: function(xhr, ajaxOptions, thrownError){
console.log("error");
}
});
});
});
</script>
@endpush
更新 2
const planCompactView = true;
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '/updateUserPreference/${planCompactView}',
method: 'post',
/* send the csrf-token and the input to the controller */
dataType: 'JSON',
data: {},
success: function () {
console.log("done");
//do something
},error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status+" ,"+" "+ajaxOptions+", "+thrownError);
}
});
现在控制台中的错误消息是:“200,parsererror,SyntaxError:JSON 输入意外结束”
需要更改 ajax 代码段中的 url 以匹配路由文件
中定义的 url您还需要获取路由参数 plan_compact_view
的值并将其附加到 js 代码段
@push('scripts')
<script type="text/javascript">
const planCompactView = 'get the value';
$(document).ready(function () {
$('#btn_view_type').click(function (e) {
//AJAX Call update user preference
$.ajax({
url: `/updateUserPreference/${planCompactView}`,
method: 'post',
dataType: 'JSON',
success: function () {
console.log("done");
},error: function(xhr, ajaxOptions, thrownError){
console.log("error");
}
});
});
});
</script>
@endpush