skip to Main Content

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


  1. You forgot to add the CSRF token:

    <form class="card" id="frmProject">
        @csrf
    

    Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user. Thankfully, Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks.

    See: https://laravel.com/docs/10.x/csrf

    Login or Signup to reply.
  2. I think there is no issue with the CSRF Token in your project.
    I just recommend 3 cases:

    • First, please confirm you are running the web application in development mode. so that you can check the error log when running.
    • Second, please check the Category model. I am wondering if there is another required field on the Category model.
    • On javascript code, you can remove isNew == true condition to check
    if (isNew == true) {
           _url = '{{ route('store') }}';
           _data = $('#frmProject').serialize();
           _method = 'POST';
          
    
    }
    
    Instead of the above,
    Just like:
    
    _url = '{{ route('store') }}';
    _data = $('#frmProject').serialize();
    _method = 'POST';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search