Laravel 中的 PATCH AJAX 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27914559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PATCH AJAX Request in Laravel
提问by Rafael
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).
是否可以向 Laravel 发出 AJAX PATCH 请求,或者我是否仅限于 POST?Laravel 在输入隐藏字段中使用 PATCH,但是,我没有使用表单元素——只是在点击时应该部分更新记录的按钮(通过 AJAX 请求)。
How would the route look like for this?
这条路线会是什么样子?
Routes file
路由文件
Route::patch('questions/{id}', 'QuestionController@update')->before('admin');
Route::patch('questions/{id}', 'QuestionController@update')->before('admin');
I am not sure if laravel routes support PATCH.
我不确定 laravel 路由是否支持 PATCH。
Controller
控制器
public function update($id) {
if (Request::ajax() && Request::isMethod('patch')) {
//partially update record here
}
}
JS
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!
我只是在寻找清晰度,谢谢!
回答by Set Kyar Wa Lar
Yeah, it's possible try
是的,可以试试
In Your JavaScript
在你的 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
在你的 QuestionController 控制器的更新方法中
dd(Request::method());
You will see the respond like
你会看到这样的回应
string(5) "PATCH"
Read more about Request Information on Laravel doc.
阅读有关Laravel 文档的请求信息的更多信息。