Laravel表单提交报错:POST方法不支持tickets路由,支持GET、HEAD
问题
提交表单时通过.on('submit', function (event)触发请求,出现错误提示:
"The POST method is not supported for route tickets/BDkkh9ef. Supported methods: GET, HEAD"
所有tickets相关路由均出现该错误,无法定位原因。
相关代码
路由定义
Route::post('tickets/update', [SupportTicketController::class, 'update'])->name('tickets.update'); Route::resource('tickets', SupportTicketController::class)->except(['destroy', 'create', 'update']); Route::post('tickets/{ticket}/assigned', [EmployeeAssignedController::class, 'employeeTicketAssigned'])->name('tickets.assigned'); Route::get('tickets/{id}/delete', [SupportTicketController::class, 'destroy'])->name('tickets.destroy'); Route::get('tickets/download/{id}', [SupportTicketController::class, 'download'])->name('tickets.downloadTicket'); Route::post('tickets/delete/selected', [SupportTicketController::class, 'delete_by_selection'])->name('mass_delete_tickets'); Route::post('tickets/{ticket}/comments', [SupportTicketCommentController::class, 'index'])->name('ticket_comments.index'); Route::post('tickets/store_comments/{ticket}', [SupportTicketCommentController::class, 'store'])->name('ticket_comments.store'); Route::get('tickets/{id}/delete_comments', [SupportTicketCommentController::class, 'destroy'])->name('ticket_comments.destroy'); Route::post('tickets/{ticket}/details', [SupportTicketController::class, 'detailsStore'])->name('ticket_details.store'); Route::post('tickets/{ticket}/notes', [SupportTicketController::class, 'notesStore'])->name('ticket_notes.store');
视图JavaScript代码
$('#assigned_form').on('submit', function (event) { event.preventDefault(); $.ajax({ url: "{{ route('tickets.assigned',$ticket) }}", method: "post", data: new FormData(this), contentType: false, cache: false, processData: false, dataType: "json", success: function (data) { let html = ''; if (data.errors) { html = '<div class="alert alert-danger">'; for (let count = 0; count < data.errors.length; count++) { html += '<p>' + data.errors[count] + '</p>'; } html += '</div>'; } if (data.success) { html = '<div class="alert alert-success">' + data.success + '</div>'; } $('#assigned_result').html(html).slideDown(300).delay(500).slideUp(300); } }) });
控制器代码
class EmployeeAssignedController extends Controller { public function employeeTicketAssigned(Request $request, SupportTicket $ticket) { $role = Role::find(Auth::user()->role_id); if($role->hasPermissionTo('assign-ticket')) { $employees = $request->input('employee_id'); $ticket->assignedEmployees()->sync($employees); $notificable = User::where('role_id', 1) ->orWhere('id', $ticket->employee->id) ->orWhere('id', $ticket->customer->id) ->orwhereIntegerInRaw('id', $employees) ->get(); Notification::send($notificable, new TicketAssignedNotification($ticket)); return response()->json(['success' => __('تم إدخال البيانات بنجاح')]); } return response()->json(['success' => __('غير مصرح لك')]); } }
排查与解决
1. 确认请求URL正确性
打开浏览器开发者工具的Network标签,查看AJAX请求的实际URL:
- 如果请求的是
tickets/BDkkh9ef而非tickets/BDkkh9ef/assigned,说明路由生成错误。检查视图中$ticket变量是否正确传递,或route('tickets.assigned', $ticket)是否生成了带/assigned后缀的路径。
2. 路由匹配与缓存
- 执行
php artisan route:list,确认tickets/{ticket}/assigned的POST路由存在。 - 若修改过路由,执行
php artisan route:clear清除路由缓存,避免旧路由规则干扰。
3. 模型绑定检查
如果SupportTicket的主键是字符串格式(如BDkkh9ef),需在模型中添加以下配置:
public $keyType = 'string'; public $incrementing = false;
否则Laravel会尝试将主键转为整数,导致模型绑定失败,请求匹配到GET tickets/{ticket}路由,触发方法不支持错误。
4. CSRF令牌验证
AJAX POST请求必须携带CSRF令牌:
- 确保表单中包含
<input type="hidden" name="_token" value="{{ csrf_token() }}">,使用FormData提交时会自动包含该字段; - 若未包含,可在AJAX的
data中手动添加_token: "{{ csrf_token() }}"。
内容的提问来源于stack exchange,提问作者Hagar Maher
相关产品推荐
相关产品推荐

