Laravel可排序的ajax与csrf“ 500内部服务器错误”

时间:2019-08-08 22:10:06

标签: ajax laravel csrf jquery-ui-sortable

当尝试更新jquery ui可排序表时,我使用ajax POST方法遇到“ 500内部服务器错误”。我也用ajax发送csrf信息。我该如何解决?

我尝试将ajax方法从POST更改为PATCH和PUT,从而相应地更改了路由功能。还是没有运气。

路线:

Route::post('/physicians/updateOrder', 'PhysiciansController@updateOrder')->name('physicians.updateOrder');

控制器:

public function updateOrder(Request $request)
{
    $tasks = Physician::all();
    foreach ($tasks as $task) {
        $task->timestamps = false; // To disable update_at field updation
        $id = $task->id;

        foreach ($request->order as $order) {
            if ($order['id'] == $id) {
                $task->update(['order' => $order['position']]);
            }
        }
    }

    return response('Update Successfully.', 200);
}

AJAX:

$(function() {
    $( "#sortable" ).sortable({ 
        placeholder: "ui-sortable-placeholder",
        items: "tr",
        cursor: 'move',
        opacity: 0.6,
        update: function() {
            sendOrderToServer();
        }
    });

    function sendOrderToServer() {

    var order = [];
    $('tr.row1').each(function(index,element) {
        order.push({
            id: $(this).attr('data-id'),
            position: index+1
        });
    });

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "{{ route('physicians.updateOrder') }}",
        data: {
        order:order,
        _token: '{{csrf_token()}}'
        },
        success: function(response) {
            if (response.status == "success") {
            console.log(response);
            } else {
            console.log(response);
            }
        }
    });

    }
});

0 个答案:

没有答案