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

MGanjKhani's avatar

405 http error on post request

I have a post request with ajax, but in response I get 405 error: get method not allowded, only post supported! My route has only one post for this request. Also I check all middlewares and routing files but the error starts from client or index.php and I can't debug it. Exactly Laravel changes my request from post to get. In other hand when I change some of my posted parameters, the request will be OK and I get 200. When I undo change the response remains on 200! I use Laravel 5.8. Any idea?

0 likes
7 replies
MGanjKhani's avatar

routes/web.php:

Route::post('add-wizard', 'PagesController@addWizard')->middleware('auth')->name('add-wizard');

app/Http/Controllers/MyController.php: (when 405 occures, code not entered here)

public function addWizard(Request $request) {
	if (!Auth::check()) {
		return response()->json(['success'=>false]);
	}

	dd($request->pfil);
}

resources/views/wizard/wizard.php

$.ajax({
	method: 'POST',
	url: "{{ route('add-wizard') }}",
	headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
	data: {
		ctg: prd0,
		vip: vip0,
		knd: knd0,
		typ: typ0,
		keys: keys,
		sids: sids,
		pmmn: pmmn,
		pmmx: pmmx,
		ptmn: ptmn,
		ptmx: ptmx,
		pbrd: pbrd,
		pimg: pimgs,
		pfil: files,
		when: unixtime,
		ppur: ppur+arr[3],
		src: $('#src').val(),
		eml: $('#eml').val(),
		pcol: $('#pcol').val(),
		pgrd: $('#pgrd').val(),
		pstt: $('#pstt').val(),
		porg: $('#porg').val(),
		pmt1: $('#mt1').val(),
		ppck: $('#ppck').val(),
		whcn: $('#whcn').val(),
		whrg: $('#whrg').val(),
		whct: $('#whct').val(),
		ptrm: $('#ptrm').val(),
		ptus: $('#ptus').val(),
		pmod: $('#pmod').val(),
		ppck2: $('#ppck2').val(),
		ppur1: $('#ppur1').val(),
		edit_mode: '{{ $edit_mode }}',
		peml: $('#peml').prop('checked')
	},
	success: function(result) {

	},
	error: function(jqXHR, textStatus, errorThrown) {
	
	}
});
rodrigo.pedra's avatar

can you try adding this header:

headers: {
    'X-Requested-With': 'XMLHttpRequest', // <<<<< add this one
    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') // this you already have
},

Issue can be:

If authentication or validation fails, Laravel checks if the request is a regular request or a API request.

If it thinks it is a regular request it then redirects back or redirect to the login page with some error variables. If it is a Ajax request Laravel responds with a JSON payload.

If the request is made through Ajax but even though Laravel thinks it is a regular one, it will send a redirect response on failure, the browser will try to follow the redirect while keeping the same HTTP verb, there it could be the reason why you are getting this error.

One way Laravel tries to assess if a request is a Ajax request is by looking into the request headers and verifying the X-Requested-With header.

MGanjKhani's avatar

I already have this in my request:

Accept
	*/*
Accept-Encoding
	gzip, deflate, br
Accept-Language
	en-US,en;q=0.5
Connection
	keep-alive
Content-Length
	909
Content-Type
	application/x-www-form-urlencoded; charset=UTF-8
Cookie
	........
Host
	www......com
Origin
	https://www.....com
Referer
	https://www......com/Post-Request
TE
	Trailers
User-Agent
	Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0
X-CSRF-TOKEN
	.................................
X-Requested-With
	XMLHttpRequest
MGanjKhani's avatar
MGanjKhani
OP
Best Answer
Level 1

Thanks to @neilstee. We checked the code and found that the $_REQUEST, $_GET and $_POST are empty in the index.php in Laravel. It was because sending some unicode characters to server and that bug was from PHP and nginx. Laravel receives no response and we must check other layers of code.

rodrigo.pedra's avatar

Thanks! But without you asking the code first I wouldn't think on suggesting to check the request's headers.

it is a team effort =)

Best part is @mganjkhani was able to find the issue.

2 likes

Please or to participate in this conversation.