Laravel 5.2中使用AJAX调用的方法不允许(PUT) [英] Method not allow (PUT) With AJAX Call in Laravel 5.2
问题描述
我的剑是:
{!! Form::open(['method' => 'PUT', 'id' => 'confirmTCU',
'action' => ['TournamentUserController@confirmUser', $tournament->slug, $categoryTournament->id,$user->slug ]]) !!}
它会生成我的表格:
<form method="POST" action="http://laravel.dev/tournaments/bisque/categories/1/users/admin/confirm" accept-charset="UTF-8" id="confirmTCU">
<input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="tiaIHtctMbo1NwbEK8TqoKOyrN8ZSyeQELSyYL9A">
<button type="submit" class="btn btn-flat text-warning-600 btnConfirmTCU" id="confirm_bisque_1_admin" data-tournament="bisque" data-category="1" data-user="admin">
<i class="text-danger glyphicon glyphicon-remove-sign"></i>
</button>
</form>
我的AJAX是:
$('.btnConfirmTCU').on('click', function (e) {
e.preventDefault();
$(this).prop("disabled", true);
var inputData = $('#formDeleteTCU').serialize();
//var tournamentSlug = $(this).data('tournament');
var categoryId = $(this).data('category');
var userSlug = $(this).data('user');
$.ajax(
{
type: 'PUT',
url: url + '/categories/' + categoryId + '/users/' + userSlug + '/confirm',
data: inputData,
success: function (data) {
...
},
error: function (data) {
...
}
}
)
});
我的路线是:
Route::put('tournaments/{tournamentId}/categories/{categoryTournamentId}/users/{userId}/confirm', 'TournamentUserController@confirmUser');
我的控制器是:
public function confirmUser($tournamentSlug, $tcId, $userSlug)
{
$user = User::findBySlug($userSlug);
$ctu = CategoryTournamentUser::where('category_tournament_id', $tcId)
->where('user_id', $user->id)->first();
$ctu->confirmed ? $ctu->confirmed = 0 : $ctu->confirmed = 1;
$ctu->save();
return redirect("tournaments/$tournamentSlug/users");
}
我看到了很多关于它的主题,但是没有一个解决我的问题.
I saw a lot of topics about it, but none resolved my issue.
由于大多数浏览器均不允许使用PUT,因此Laravel像POST一样发送它,但包含一个具有PUT值的隐藏字段_method.
As PUT is not allowed for most of browser, Laravel send it like POST, but includes a hidden field _method with PUT value.
此外,我可以执行DELETE操作,但不能执行PUT ...
Beside, I am able to perform DELETE actions, but not PUT...
此外,该方法在不使用AJAX时效果很好.
Besides, method works perfect when not using AJAX.
我的问题在哪里?
推荐答案
您的代码应该可以正常工作,但是看起来您正在序列化错误的格式.您当前的代码显示为var inputData = $('#formDeleteTCU').serialize();
,但是您显示的表单的ID为confirmTCU
.
Your code should work fine, but it looks like you are serializing the wrong form. Your current code shows var inputData = $('#formDeleteTCU').serialize();
, but the id for the form you've shown is confirmTCU
.
这篇关于Laravel 5.2中使用AJAX调用的方法不允许(PUT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!