[英]The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. while adding a field
[英]message: The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE
所以我有一個 API 路由
Route::group(['prefix' => 'users'], function() {
Route::group(['prefix' => 'seminar'], function() {
Route::get('/{employee_number}', [UserProfileController::class, 'getSeminar']);
Route::post('/{user}', [UserProfileController::class, 'createSeminar']);
Route::put('/{seminar}', [UserProfileController::class, 'updateSeminar']);
Route::delete ('/{seminar}', [UserProfileController::class, 'deleteSeminar']);
});
});
和一個控制器
public function createSeminar(User $user, Request $request)
{
return $this->allowIfRecordOwner($user->id, function() use ($user, $request) {
$seminar = Seminar::create([
"user_id" => $user->id,
"dates" => $request->dates,
"name" => $request->name,
"certificate_number" => $request->certificate_number
]);
return response()->json($seminar->toArray(), 200);
});
}
我在我的角度應用程序中使用它
private saveSeminar(index) {
event.preventDefault();
const seminar = this.userSeminars[index];
if (seminar.id) {
this.updateUserSeminar(index);
} else {
this.storeStudentAddress(index);
}
}
private storeStudentAddress(index) {
this.apiService.create('users/seminar', this.userSeminars[index])
.subscribe(
response => {
this.userSeminars[index].edit = false;
this.userSeminars[index].id = response.id;
this.getUserSeminar(index.employee_number);
this.toastr.success('Seminar Successfully saved', 'Success');
});
}
我已經完成了php artisan route:list
並在這里找到了我的路由器
我現在已經盯着這個錯誤看了三個多小時,不明白為什么我會收到這個錯誤。 任何幫助都會很棒
Laravel 中的 API 端點看起來與 JavaScript 中的 API 端點不一致。
您需要點擊此端點才能創建研討會: api/users/seminar/{user}
,其中{user}
是用戶的 ID。
您在此處的行: this.apiService.create('users/seminar', this.userSeminars[index])
看起來像是在點擊api/users/seminar
,而沒有在末尾附加用戶 ID。
您只需將其更改為this.apiService.create('users/seminar/' + USER_ID, ...
其中USER_ID
是您用於存儲用戶 ID 的任何變量,或者返回它的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.