Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

AJAX post: MethodNotAllowedHttpException No message laravel 5.4

Hi can someone help me??please, I'm having problem with adding data when I'm trying to call 'schools-list.store' route not works when I'm hit the save button it redirect to 'next page' instead of 'schools-list.store' or 'ajax' whats wrong??

schools.blade.php

<form id="school-forms" method="post">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label class="col-md-3" for="province_name">province_name</label>
                <div class="col-md-9">
                    <select id="province_name" name="province_id" class="form-control col-md-12" required>
                        @foreach($province_names as $province_name)
                            <option value="{{ $province_name->id }}">{{ $province_name->province_name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="city_name">city_name</label>
                <div class="col-md-9">
                    <select id="city_name" name="city_id" class="form-control col-md-12" required>

                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="center_name">center_name</label>
                <div class="col-md-9">
                    <select id="center_name" name="center_id" class="form-control col-md-12" required>

                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="national_number_founder">national_number_founder</label>
                <div class="col-md-9">
                    <input type="text" id="national_number_founder" name="national_number_founder" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="school_name">school_name</label>
                <div class="col-md-9">
                    <input type="text" id="school_name" name="school_name" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="status">status</label>
                <div class="col-md-9">
                    <div class="radio">
                        <label>
                            <input type="radio" name="status" id="1" value="1">                        active
                        </label>
                        <label>
                            <input type="radio" name="status" id="0" value="0">                      deactive
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="gender">gender</label>
                <div class="col-md-9">
                    <div class="radio">
                        <label>
                            <input type="radio" name="gender" id="male" value="1">                        male
                        </label>
                        <label>
                            <input type="radio" name="gender" id="female" value="2">                      female
                        </label>
                        <label>
                            <input type="radio" name="gender" id="female" value="3">                      all
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="note">note</label>
                <div class="col-md-9">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="note[]" value="34"> 34
                        </label>
                        <span class="col-sm-12"></span>
                        <label>
                            <input type="checkbox" name="note[]" value="35"> 35
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="type">type</label>
                <div class="col-md-9">
                    <input type="text" id="type" name="type" class="form-control col-md-12">
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label class="col-md-3" for="file_number">file_number</label>
                <div class="col-md-9">
                    <input type="text" id="file_number" name="file_number" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="phone">phone</label>
                <div class="col-md-9">
                    <input type="text" id="phone" name="phone" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="address">address</label>
                <div class="col-md-9">
                    <input type="text" id="address" name="address" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="postal_code">postal_code</label>
                <div class="col-md-9">
                    <input type="text" id="postal_code" name="postal_code" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="building">building</label>
                <div class="col-md-9">
                    <input type="text" id="building" name="building" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="area">area</label>
                <div class="col-md-9">
                    <input type="text" id="area" name="area" class="form-control col-md-12">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3" for="date_establishment">date_establishment</label>
                <div class="col-md-9">
                    <input type="text" id="date_establishment" name="date_establishment" class="form-control col-md-12">
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="wizard-buttons">
            <button id="save-activity" type="submit" class="btn btn-next2 col-md-4 btn-lg">
                <span class="glyphicon glyphicon-ok" aria-hidden="true"></span> save & continue
            </button>
        </div>
    </div>
</form>

Ajax

$("#school-forms").submit(function (event) {
    event.preventDefault();
    province_id = $('#province_id').val();
    city_id = $('#city_id').val();
    center_id = $('#center_id').val();
    national_number_founder = $('#national_number_founder').val();
    school_name = $('#school_name').val();
    status = $('#status').val();
    gender = $('#gender').val();
    note = $('#note').val();
    type = $('#type').val();
    file_number = $('#file_number').val();
    phone = $('#phone').val();
    address = $('#address').val();
    postal_code = $('#postal_code').val();
    building = $('#building').val();
    date_establishment = $('#date_establishment').val();
    $.post("{{ route('schools-list.store') }}", {province_id:province_id, city_id:city_id, center_id:center_id, national_number_founder:national_number_founder, school_name:school_name, status:status, gender:gender, note:note, type:type, file_number:file_number, phone:phone, address:address, postal_code:postal_code, building:building, date_establishment:date_establishment}, function (data_school) {
        $('#province_id').val('');
        $('#city_id').val('');
        $('#center_id').val('');
        $('#national_number_founder').val('');
        $('#school_name').val('');
        $('#status').val('');
        $('#gender').val('');
        $('#note').val('');
        $('#type').val('');
        $('#file_number').val('');
        $('#phone').val('');
        $('#address').val('');
        $('#postal_code').val('');
        $('#building').val('');
        $('#date_establishment').val('');
        console.log(event);
    }, 'json');
});

Database

    Schema::create('schools', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('province_id')->unsigned();
        $table->integer('city_id')->unsigned();
        $table->integer('center_id')->unsigned();
        $table->string('national_number_founder');
        $table->string('school_name');
        $table->boolean('status');
        $table->boolean('gender');
        $table->integer('note');
        $table->string('type');
        $table->string('file_number');
        $table->string('phone');
        $table->string('address');
        $table->string('postal_code');
        $table->string('building');
        $table->string('area');
        $table->string('date_establishment');
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('province_id')->references('id')->on('provinces');
        $table->foreign('city_id')->references('id')->on('cities');
        $table->foreign('center_id')->references('id')->on('centers');
    });
}

SchoolListController.php

class SchoolsListController extends Controller
    {
        public function index()
        {
            $schools_lists = SchoolsList::where('user_id', Auth::user()->id)->latest()->paginate(25);
            return view('Admin.submit-information.all', compact('schools_lists'));
        }
        public function read_schools()
        {
            $schools = SchoolsList::all();
            return response()->json($schools);
        }
        public function findIDProvince(Request $request)
        {
            $data = City::select('city_name', 'id')->where('province_id', $request->id)->take(100)->get();
            return response()->json($data);
        }
        public function findIDCity(Request $request)
        {
            $data = Center::select('center_name', 'id')->where('city_id', $request->id)->take(100)->get()->toArray();
            return response()->json($data);
        }
        public function create()
        {
            $province_names = Province::all();
            return view('Admin.submit-information.create', compact('province_names'));
        }
        public function store(Request $request)
        {
            if($request->ajax()) {
                $school = new SchoolsList();
                $school->user_id = auth()->user()->id;
                $school->province_id = $request->province_id;
                $school->city_id = $request->city_id;
                $school->center_id = $request->center_id;
                $school->national_number_founder = $request->national_number_founder;
                $school->school_name = $request->school_name;
                $school->status = $request->status;
                $school->gender = $request->gender;
                $school->note = $request->note;
                $school->type = $request->type;
                $school->file_number = $request->file_number;
                $school->phone = $request->phone;
                $school->address = $request->address;
                $school->postal_code = $request->postal_code;
                $school->building = $request->building;
                $school->date_establishment = $request->date_establishment;
                $school->save();
                return response()->json(['data_school' => $request->all(), 'id' => $school->id]);
            }
        }
    }

web.php

$this->get('/findIDCity', 'SchoolsListController@findIDCity');
$this->get('/findIDProvince', 'SchoolsListController@findIDProvince');
$this->resource('schools-list', 'SchoolsListController');

When I click on save I get this 405 error in network.

 MethodNotAllowedHttpException

0 likes
41 replies
Cronix's avatar

Not sure why. Your code looks correct (except I don't see you sending a csrf token?)

$.post("{{ route('schools-list.store') }}"

However, for some reason, you'll see it's actually sending it to the /schools-list/create url instead of just /schools-list.

You could try manually entering the url instead of using blade and the route helper there (which shouldn't be in javascript code anyway)

so something like

$.post("/schools-list"

//or
$.post("/admin/schools-list"
mozew's avatar
Level 6

I replaced with it

 $.post("/admin/schools-list", {province_id:province_id,.......

It not worked

What's the solution?

mozew's avatar
Level 6

Thank you for taking the time

Cronix's avatar

I don't know. Whats the error now? When looking at your dev tools (like you showed in your screenshot), is the request going to /admin/schools-list now instead of /admin/schools-list/create?

Nakash's avatar

Hi,

I think @Cronix is right to talk about the csrf token. The last time I met this issue, it fixed it. You should add it anyway.

mozew's avatar
Level 6

I addedd csrf token after form tag. get MethodNotAllowedHttpException error again.

<form id="school-forms" method="post">
    {{ csrf_token() }} 
Cronix's avatar

That doesn't add it to the ajax request. Did you check the url request in dev tools like I mentioned to see if it's going to the correct place now? If not, have you tried clearing your browser cache?

mozew's avatar
Level 6

How to clearing my browser cache? With following line:

php artisan cache:clear

Is it correct?

What is dev tools? Thank you for my answer. It is a project very important.

Cronix's avatar

What is dev tools?

Your browsers dev tools, like the last image you showed in your first post showing the request failing.

Is it showing the request going to /admin/schools-list?

How to clearing my browser cache? With following line:

No, your browsers cache, not laravels cache. It could be that your javascript is cached in the browser and using your old code and not your new code. It could do that if your javascript is in a separate file from the view. I don't know your setup, but it never hurts to clear your browsers cache. It depends on your browser, and OS so I don't know how to tell you to do it.

Edit: I just noticed in your dev tools, there is an option to "disable cache". Its a checkbox near the upper-right of the last image you're showing in the first post.

mozew's avatar
Level 6
Route::namespace('Admin')->prefix('admin')->middleware('auth')->group(function (){
        $this->get('/findIDCity', 'SchoolsListController@findIDCity');
        $this->get('/findIDProvince', 'SchoolsListController@findIDProvince');
        $this->resource('schools-list', 'SchoolsListController');
});

I tried 10 hours but I get 'MethodNotAllowedHttpException' error.

jlrdw's avatar

get the csrf token in your Ajax request. many other examples of doing that on the forum.

where you placed it is for a normal submittal of a form without Ajax.

it needs to be passed in your Ajax also.

mozew's avatar
Level 6

How to use csrf token. I tried all possible solutions, I became very confused...

What should I do !?

Inquisitive's avatar
date_establishment:date_establishment, _token: csrf_token }

But before that you need to define csrf_token somewhere. With something like below

var csrf_token = "{{csrf_token()}}";

eg: In view php file you can add

  <script type="text/javascript">    
    var csrf_token = "{{csrf_token()}}";
  </script>

I don't think you are passing csrf token. You have placed input field in form but you are not sending it.

Or you need to retreive that value from the form and need to send it

Cronix's avatar

Answer my question.

Hmm, you still haven't answered whether the ajax request is going to the correct url now... So, I'm waiting for you to answer mine

Whether the token is being sent or not is not the cause of "MethodNotAllowedHttpException"

mozew's avatar
Level 6

No, it ajax request isn,t going to the correct url.

But I added dd() in store yet I see MethodNotAllowedHttpException error.

public function store(Request $request)
    {
        dd($request->all());
}
Cronix's avatar

Yes, if the method isn't allowed, you can't access the method so the dd() won't do anything.

No, it ajax request isn,t going to the correct url.

Sooooo what url is it going to now?

Cronix's avatar

Post your current javascript. It's going to the same url as it originally was.

mozew's avatar
Level 6

My javascript this is it, That you saw in the post.

How to post my current javascript. What should I do?

Cronix's avatar

Your original javascript was posting to the wrong url in the ajax request. I'm trying to see if you actually changed it because it's still going to that same wrong url.

Cronix's avatar

try wrapping all of your js in a domready event (it should always be).

<script>
$(function() {
    // put all of your javascript in here.
});
</script>
mozew's avatar
Level 6

I tried put all of your javascript.

<script type="text/javascript">
    $(function() {
        function load_schools() {
            $.get('read-schools', function (data_school){
                $('#data_school').html("");
                $.each(data_school, function (key, val) {
                    $('#data_school').append("<tr>"+
                        "<td>"+val.id+"</td>"+
                        "<td id='eprovince_id'>"+val.province_id+"</td>"+
                        "<td id='ecity_id'>"+val.city_id+"</td>"+
                        "<td id='ecenter_id'>"+val.center_id+"</td>"+
                        "<td id='enational_number_founder'>"+val.national_number_founder+"</td>"+
                        "<td id='eschool_name'>"+val.school_name+"</td>"+
                        "<td id='estatus'>"+val.status+"</td>"+
                        "<td id='egender'>"+val.gender+"</td>"+
                        "<td id='enote'>"+val.note+"</td>"+
                        "<td id='etype'>"+val.type+"</td>"+
                        "<td id='efile_number'>"+val.file_number+"</td>"+
                        "<td id='ephone'>"+val.phone+"</td>"+
                        "<td id='eaddress'>"+val.address+"</td>"+
                        "<td id='epostal_code'>"+val.postal_code+"</td>"+
                        "<td id='ebuilding'>"+val.building+"</td>"+
                        "<td id='edate_first_establishment'>"+val.date_first_establishment+"</td>"+
                        "<td>"+
                        "<a id='edit_school' data-id="+val.id+"><span class='glyphicon glyphicon-pencil'></span></a>"+
                        "<a id='delete_school' data-id="+val.id+"><span class='glyphicon glyphicon-remove'></span></a>"+
                        "</td>"+
                        +"</tr>"
                    )
                });
            });
        }
        load_schools();
        $("#schools").submit(function (event) {
            event.preventDefault();
            province_id = $('#province_id').val();
            city_id = $('#city_id').val();
            center_id = $('#center_id').val();
            national_number_founder = $('#national_number_founder').val();
            school_name = $('#school_name').val();
            status = $('#status').val();
            gender = $('#gender').val();
            note = $('#note').val();
            type = $('#type').val();
            file_number = $('#file_number').val();
            phone = $('#phone').val();
            address = $('#address').val();
            postal_code = $('#postal_code').val();
            building = $('#building').val();
            date_establishment = $('#date_establishment').val();
            $.post("{{ route('schools-list.store') }}", {province_id:province_id, city_id:city_id, center_id:center_id, national_number_founder:national_number_founder, school_name:school_name, status:status, gender:gender, note:note, type:type, file_number:file_number, phone:phone, address:address, postal_code:postal_code, building:building, date_establishment:date_establishment}, function (data_school) {
                var csrf_token = "{{csrf_token()}}";
                $('#province_id').val('');
                $('#city_id').val('');
                $('#center_id').val('');
                $('#national_number_founder').val('');
                $('#school_name').val('');
                $('#status').val('');
                $('#gender').val('');
                $('#note').val('');
                $('#type').val('');
                $('#file_number').val('');
                $('#phone').val('');
                $('#address').val('');
                $('#postal_code').val('');
                $('#building').val('');
                $('#date_establishment').val('');
                console.log(event);
                load_schools();
            }, 'json');
        });
    });
</script>

But I see yet MethodNotAllowedHttpException error.

Cronix's avatar

You're still sending to the same url and haven't changed it like I suggested. I'm out of ideas then. I'm sorry.

mozew's avatar
Level 6

How to post my current javascript. What should I do?

mozew's avatar
Level 6

@Cronix

How about this? Does it answer? I put.

var csrf_token = "{{csrf_token()}}";
Cronix's avatar

No, that's a separate problem and won't change the problem with the ajax request going to the wrong url.

Is this javascript in a laravel blade file, or is it a standalone .js file that's getting loaded?

Cronix's avatar

Ok, but that doesn't answer my question.

Just tell me the filename where your js is located in. Is it like /public/js/myjavascript.js, or is it like /resources/views/some.blade.php?

Next

Please or to participate in this conversation.