The post method is not supported for route admin/categories/create. supported methods: get, head.

  Wed 20 / 09 / 2023

  Posted by: Muhammad Siddique

Category : Laravel

Tags : laravel , php , php laravel

Post Thumbnail
Description

Please help me to solve the issue. I have tried many methods to solve it but failed. This is the issue that I can face again and again.

(The POST method is not supported for route admin/categories/create. Supported methods: GET, HEAD).


View Code
                        @extends('admin.layouts.app')

@section('content')
    	<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<div class="container-fluid my-2">
						<div class="row mb-2">
							<div class="col-sm-6">
								<h1>Create Category</h1>
							</div>
							<div class="col-sm-6 text-right">
								<a href="categories.html" class="btn btn-primary">Back</a>
							</div>
						</div>
					</div>
					<!-- /.container-fluid -->
				</section>
				<!-- Main content -->
				<section class="content">
					<!-- Default box -->
					<div class="container-fluid">
						<form action="" method="POST" id="categoryForm" name="categoryForm">
                            <div class="card">
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="mb-3">
                                                <label for="name">Name</label>
                                                <input type="text" name="name" id="name" class="form-control" placeholder="Name">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3">
                                                <label for="slug">Slug</label>
                                                <input type="text" name="slug" id="slug" class="form-control" placeholder="Slug">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3">
                                                <label for="status">Status</label>
                                                <select name="status" id="status" class="form-control">
                                                    <option value="1">Active</option>
                                                    <option value="0">Block</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="pb-5 pt-3">
                                <button type="submit" class="btn btn-primary">Create</button>
                                <a href="brands.html" class="btn btn-outline-dark ml-3">Cancel</a>
                            </div>
                        </form>
					</div>
					<!-- /.card -->
				</section>
				<!-- /.content -->
			</div>
			<!-- /.content-wrapper -->
@endsection

@section('customJs')
<script>
    $(#categoryForm).submit(function (e) {
        e.preventDefault();
        var element=$(this);
        $.ajax({
            type: "post",
            url: "{{ route('categories.store') }}",
            data: element.serializeArray(),
            dataType: "json",
            success: function (response) {

            },
            error: function(jqXHR,exception){
                console.log('Something went wrong');
            }
        });
    });
</script>
@endsection


  Route::get('admin/categories/create',[CategoryController::class,'create'])->name('categories.create');
  Route::post('admin/categories',[CategoryController::class,'store'])->name('categories.store');
                
  Answered by CDL

Yes it's happening becuase you are submitting your post request on get route, so you need to send on the post route then it will work.

For now you are sending your request through ajax but in the ajax you are using post but it's submitting the html form not the ajax form and your html form contains the empty action attribute. You can fix this issue by changing with click instead of submit.


$(#categoryForm).click(function (e) {


});


Solved
  Comment   Share
0 Likes   0 Comments