i am creating the simple crud application using laravel using ajax.when i tring to add the data i got the error was POST419 (unknown status) Laravel what i tried so far i attached below.
i have implemented csrf token inside the head tag
<meta name="csrf-token" content="{{ csrf_token() }}">
Ajax and jQuery
function addProject() {
if ($("#frmProject").valid())
{
var _url = '';
var _data = '';
var _method;
if (isNew == true) {
_url = '{{ route('store') }}';
_data = $('#frmProject').serialize();
_method = 'POST';
}
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: _method,
url: _url,
dataType: 'JSON',
data: _data,
cache : false,
contentType:false,
processData:false,
Form
<form class="card" id="frmProject">
<div class="form-group" align="left">
<label class="form-label">Category Name</label>
<input type="text" class="form-control" placeholder="Category Name" id="categoryname" name="categoryname" size="30px" required>
</div>
<div class="form-group" align="left">
<label class="col-sm-2 control-label">Status</label>
<select class="form-control" id="status" name="status"
placeholder="Project Status" required>
<option value="">Please Select</option>
<option value="1">Active</option>
<option value="2">DeActive</option>
</select>
</div>
<div class="card" align="right">
<button type="button" id="save" class="btn btn-info" onclick="addProject()">Add
</button>
<button type="button" id="clear" class="btn btn-warning" onclick="reSet()">Reset</button>
</di
v>
</form>
Route
Route::post(‘/store’, [CategoryController::class, ‘store’])->name(‘store’);
Controller
public function store(Request $request)
{
$catData = [
'categoryname' => $request->categoryname,
'status' => $request->status,
];
Category::create($catData);
return response()->json([
'status' => 200,
]);
}
Model
class Category extends Model
{
protected $fillable = [
'categoryname',
'status'
];
use HasFactory;
}
2
Answers
You forgot to add the CSRF token:
See: https://laravel.com/docs/10.x/csrf
I think there is no issue with the CSRF Token in your project.
I just recommend 3 cases: