我正在更新数据库中某些模型的值,但是当我单击“提交”时,出现以下错误:
“消息:”,例外: “ Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException”, 文件: “ C:\ xampp \ htdocs \ Restaurante1 \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php”, 行:255“
这是我的JQuery代码:
var formData = new FormData();
formData.append('id', $(this).attr("data-id"));
formData.append('first', $('#schedules_morning').val());
formData.append('second', $('#schedules_night').val());
$.ajax({
async: true,
cache: false,
url: '/schedules',
type: 'PUT',
data:
{
formData
},
dataType: 'JSON',
processData: false,
contentType: false,
success: function (data) {
$('.form_valid_container').fadeIn().html('<span class="form_valid_text">✓ '+ data.success +'</span>');
form.trigger("reset");
console.log(data.success);
},
error: function (data){
var errors = data.responseJSON;
console.log(errors);
$.each(errors , function(){
$('.form_error_container').fadeIn().html('<span class="form_error_text">✘ '+ errors.message +'</span>')
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我正在将请求发送到资源控制器路由:
Route :: resource('/ schedules','ScheduleController');
public function update(Request $request)
{
$schedule = Schedule::findOrFail($id);
$schedule->morning = $request->morning;
$schedule->night = $request->night;
$schedule->save();
return response()->json([
'schedule' => $schedule,
'success' => 'Horario actualizado correctamente',
]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 0 :(得分:0)
PHP(Laravel)只需欺骗PUT和DELETE请求,以便将所有put请求放入laravel中,然后将一个名为_method
的隐藏变量放入值put
,然后laravel再次验证该方法是否可以执行发布请求。因此,像下面这样替换您的代码,它应该可以工作。
var formData = new FormData();
formData.append('id', $(this).attr("data-id"));
formData.append('first', $('#schedules_morning').val());
formData.append('second', $('#schedules_night').val());
formData.append('_method', 'put');
$.ajax({
async: true,
cache: false,
url: '/schedules',
type: 'POST',
data:
{
formData
},
dataType: 'JSON',
processData: false,
contentType: false,
success: function (data) {
$('.form_valid_container').fadeIn().html('<span class="form_valid_text">✓ '+ data.success +'</span>');
form.trigger("reset");
console.log(data.success);
},
error: function (data){
var errors = data.responseJSON;
console.log(errors);
$.each(errors , function(){
$('.form_error_container').fadeIn().html('<span class="form_error_text">✘ '+ errors.message +'</span>')
});
}
});