[英]Request data of AJAX POST is null in Laravel Controller
I'm trying to send via AJAX data, I'm doing a post and then receiving it on the laravel controller.我正在尝试通过 AJAX 数据发送,我正在做一个帖子,然后在 laravel 控制器上接收它。
I'm getting an error that the data is null.我收到数据为空的错误。
I tried multiples ways to fix it but I'm not able to figure out how to do it.我尝试了多种方法来修复它,但我无法弄清楚如何去做。
Ajax:阿贾克斯:
$(document).ready(function () {
$('table tbody').sortable({
update: function (event, ui) {
$(this).children().each(function (index) {
if ($(this).attr('data-position') != (index + 1)) {
$(this).attr('data-position', (index + 1)).addClass('updated');
}
});
saveNewPositions();
}
});
});
function saveNewPositions() {
var positions = [];
$('.updated').each(function () {
positions.push([$(this).attr('data-index'), $(this).attr('data-position')]);
$(this).removeClass('updated');
});
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
console.log(positions);
$.ajax({
url: 'cursos',
method: 'POST',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(positions),
contentType: "application/json; charset=utf-8",
traditional: true,
})
}
Laravel Controller: Laravel 控制器:
public static function updateOrder(Request $request)
{
foreach ($request->positions as $position) {
$index = $position[0];
$newPosition = $position[1];
$seccion = SectionCourse::findOrFail($index);
$seccion->order = $newPosition;
$seccion->save();
}
return response('success', 200);
}
Doing a dd of the request, I receive this:做一个请求,我收到这个:
我直接使用$request->toArray()
而不是$request->positions
修复了它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.