PATCH AJAX Request in Laravel

ぃ、小莉子 提交于 2019-12-10 19:40:22

问题


Is it possible to make AJAX PATCH requests to laravel, or am I restricted to POST? Laravel uses PATCH in input hidden fields, however, I am not using form elements—just buttons that should partially update a record when clicked (via an AJAX request).

How would the route look like for this?

Routes file

Route::patch('questions/{id}', 'QuestionController@update')->before('admin');

I am not sure if laravel routes support PATCH.

Controller

public function update($id) {
    if (Request::ajax() && Request::isMethod('patch')) {
        //partially update record here
    }
}

JS

$('div#question_preview <some button selector>').click(function (event) {
    $.ajax({
        url: 'questions/'+question_id,
        type: 'PATCH',
        data: {status: 'some status'}
    });
});

I am just looking for clarity, thanks!


回答1:


Yeah, it's possible try

In Your JavaScript

$('#div#question_preview <some button selector>').click(function() {
        $.ajax({
                url: 'questions/'+question_id,
                type: 'PATCH',
                data: {status: <SOME VALUE I WANT>, _method: "PATCH"},
                success: function(res) {

                }
        });
});

In Your Route

Route::patch('questions/{id}', 'QuestionController@update')->before('admin');

In your QuestionController Controller's update method

dd(Request::method());

You will see the respond like

string(5) "PATCH"

Read more about Request Information on Laravel doc.



来源:https://stackoverflow.com/questions/27914559/patch-ajax-request-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!