我嘗試通過 JQuery 觸發 onClick 事件發出 ajax 請求,但是當它發送 AJAX 請求時,我收到:PATCH http://localhost:8000/courses 405(不允許方法)(當前頁面)因為它沒有獲取帶有 id 的 URL超文本標記語言@foreach ($courses as $course) <tr> <td>{{ Form::select('year', $years, ['class' => 'form-control'], [ 'placeholder' => $course->academicYear]) }}</td> <td>{{ Form::select('subject', $subjects, ['class' => 'form-control'], [ 'placeholder' => $course->subject]) }}</td> <td> <a href="" id="saveCourse" class="btn btn-success pull-left">Save</a> <input type="hidden" id="idCourse" value="{{ $course->id }}"> (...)JQuery+AJAX$('#saveCourse').click(function(e){ e.preventDefault(); var id = $('#idCourse').val(); // Ignore this logic var values = {year: "", subject:"", id: id}; var parameters = ['year', 'subject']; var i = 0; $('td > select option:selected').each(function() { values[parameters[i]] = $(this).text(); i++; }); // Ajax request $.ajax({ type: 'patch', // Appending the course id here not working, // but if i put anything else like /blabla/ + id ajax doesn't ignore it... url: '/courses/' + id, headers: {'X-CSRF-Token': csrf_token}, dataType: 'json', data: values, success: function (response) { console.log("SUCCESS: " + response); }, error: function (reject) { if( reject.status === 422 ) { $("#error").text("Los datos dados no cumplen el formato requerido."); } } }); });網頁版/* -----COURSE_ROUTES------ */ Route::resource('courses', 'CourseController')->except([ 'create', 'edit' ]);航線編輯如果我在AJAX 中使用POST而不是獲取 id。PATCHtype發現了同樣問題的 GitHub 問題https://github.com/jquery/jquery/issues/3944
4 回答

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
如下所述,405
(METHOD NOT ALLOWED)基本上意味著PATCH
服務器上的特定資源不允許使用 ajax 請求方法。
如果您使用路由庫,您可以轉到他們的文檔并搜索如何更改此行為。一個路由可以接受一種或多種請求方法,我假設默認情況下Route::resource
使用方法創建路由POST
,這說明ajax請求以POST類型工作。
- 4 回答
- 0 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消