我试图通过打开模态引导更新数据,并得到一个错误时,把数据从 AJAX 路由
这是错误'localhost:8000/nilaiUpdates/1 419(未知状态)
我试着改变了很多方法,但都不管用。
这是我的路线:Route::put('/nilaiUpdates/{id}', [PesertaController::class, 'update']);
我的刀片文件nilaiPeserta.blade.php:
<div id="editPesertaModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form id="update-user-form">
<div class="modal-header">
<h4 class="modal-title">Edit Peserta</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Name</label>
<input id="id" name="id" type="text" class="form-control" readonly >
<input id="nama" name="nama" type="text" class="form-control" required >
</div>
<div class="form-group">
<label>Email</label>
<input id="email" name="email" type="email" class="form-control" required >
</div>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" class="btn btn-info update-btn" value="Save">
</div>
</form>
</div>
</div>
</div>
我 AJAX :
错误来自这里
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
}
});
$('.update-btn').on('click', function (e) {
e.preventDefault();
let id = $('#id').val();
var data = {
'nama' : $('#nama').val(),
'email' : $('#email').val(),
}
$.ajax({
type: "PUT",
url: "/nilaiUpdates/" + id,
data: data,
dataType : 'json',
success: function (response) {
console.log(response);
},
error: function (xhr) {
console.log(xhr.responseText);
}
});
});
我的控制器:
PesertaController.php
public function update(Request $request, $id)
{
$this->validate($request,[
'nama' => 'required',
'email' => 'required',
]);
$peserta = Peserta::find($id);
$peserta->nama = $request->nama;
$peserta->email = $request->email;
$peserta->update();
return response()->json([
'status'=>200,
'message' => 'Peserta Updated Successfuly',
]);
}
我的型号:
class Peserta extends Model
{
use HasFactory;
protected $table = "peserta";
protected $fillable =
[
'nama'
,'email'
];
}
请帮帮我:)
1条答案
按热度按时间mtb9vblg1#
据我所知,当
csrf_token
get过期或丢失时,419 (unknown status)
就会出现。将此添加到
<head>
部分。也许在布局上