$.ajax() POST translating to GET

Published 1 month ago by systemdude92

Sending POST request through AJAX but getting receiving MethodNotAllowedHttpException and I do not know how to solve this issue. I've used both type AND method for the method type in the JSON object but still nothing. Used Postman to troubleshoot but get the same issue of it translating over to a GET request in Laravel.

I know that the status returns a 405 if incorrectly formated, but this is returning a status of 500 with the MethodNotAllowedHttpException

JS File:

$('#bugReportForm').submit(function(event){
    event.preventDefault();
    var subject = $('#bugSubject').val();
    var description = $('#bugDescription').val();
    $.ajax({
        url: 'bug/report',
        type: 'POST',
        data: {
            subject : subject,
            description : description
        },
        success: function(results){
            alert(results);
        },
        error: function(error){
            console.log(error);
        }
    })
})

Route :

Route::post('/bug/report', 'Layout\[email protected]');
aljo.gabot

replace the 'POST' to 'GET' on your type property and it should work.

systemdude92

unable to use GET for privacy reason

jdc1898

Are you sure that it is hitting the correct URL? Can you look at your web server log and see it actually going to the expected endpoint?

Meaning does ('bug/report' = '/bug/report')

Snapey
Snapey
1 month ago (683,015 XP)

You can get this if you need to pass a parameter in the url and you don't set it - or vice versa

systemdude92

I do not want to pass through parameter due to length of content.

@jdc1898, Web Server Logs says that its pointed to the correct URL

Snapey
Snapey
1 month ago (683,015 XP)

start the url with a backslash

tykus
tykus
1 month ago (374,760 XP)

Is this happening in production or in dev?

systemdude92

@Snapey, adding a backslash will make it a 404 error

@tykus This is only happening in Production and not dev

Snapey
Snapey
1 month ago (683,015 XP)

so what does your page url look like? does it have public in it?

systemdude92

it does not have public in it. My Web Server's sub domain points to the index in the public folder by default

Snapey
Snapey
1 month ago (683,015 XP)

so something is not right if /bug/report gives a 404 ?

tykus
tykus
1 month ago (374,760 XP)

Are you behind a load balancer? On Heroku?

I ask because I had an issue previously on Heroku where the rewrite rule for HTTPS was changing a POST request to a GET request (makes sense, right; because it was a 301 redirect?). To preserve the original request method, we need to change the redirect code to a 307, e.g.

DirectoryIndex index.php index.html index.htm

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=307]
~                                                                 
systemdude92

@Snapey I miss spoke. Doing /bug/report gives me a 500

@tykus I don't think so. We're running on a Cisco Load Balancer

Snapey
Snapey
1 month ago (683,015 XP)

I think the next step is to roll up your sleeves and open the browser inspector, network tab.

Post your ajax and check where it is posted, what the headers are, what gets returned in response and if a redirect occurs.

tykus
tykus
1 month ago (374,760 XP)

And does the load balancer handle HTTP -> HTTPS redirection? How?

Please sign in or create an account to participate in this conversation.