我尝试通过 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。PATCH
type
发现了同样问题的 GitHub 问题https://github.com/jquery/jquery/issues/3944
千万里不及你
ITMISS
犯罪嫌疑人X
梦里花落0921