Archit Singh
Archit Singh

Reputation: 212

Laravel 405 Method Not Allowed calling POST method via GET

Whenever I am trying to make an ajax call with base64 image in body it gives error as:

The GET method is not supported for this route. Supported methods: POST.

Here is my ajax call:

async function addToCart() {

        document.getElementById('preloader').style.display = 'block';
        document.getElementById('addToCartButton').style.display = 'none';
        // Get base64URL
        var image = "";
        var full_image = "";
        if (document.getElementById("category_id").value == 'customize back cover') {
            //console.log("652")
            await html2canvas(document.getElementById("html-content-holder")).then(function (canvas) {
                document.body.appendChild(canvas);
                var base64URL = canvas.toDataURL('image/jpeg').replace('image/jpeg', 'image/octet-stream');
                full_image = base64URL;
                image = document.getElementById('selectedImage').src;
            });
        }
        $.ajax({
            type: "POST",
            url: "http://emlt.thebakeologyindia.com/add_to_cart", // You add the id of the post and the update datetime to the url as well
            data: {
                _token: document.getElementById('token').value,
                user_id: document.getElementById('user_id').value,
                product_id: document.getElementById('product').value,
                quantity: document.getElementById('quantity').value,
                image: image,
                full_image: full_image
            },
            success: function (response) {
                // If not false, update the post
                //console.log(response);
                swal({
                    title: response.data.status,
                    text: response.data.message,
                    icon: response.data.status,
                });
                if(response.data.status == "error")
                {
                document.getElementById('addToCartButton').style.display = 'block';
                document.getElementById('preloader').style.display = 'none';
                }
                else
            {
                document.getElementById('itemInCartLink').style.display = 'block';
                document.getElementById('addToCartButton').style.display = 'none';
                document.getElementById('preloader').style.display = 'none';
            }
            },
            error: function (error) {
                document.getElementById('addToCartButton').style.display = 'block';
                document.getElementById('preloader').style.display = 'none';
               swal({
                    title: "Error",
                    text: error.message,
                    icon: "error",
                });
            }
        });
    }

Here is the form data example:

_token: RHJrQ4wX6XbXqi0wOx8sMiFHCuXVI2HclGB3rDco, user_id: 1, product_id: 1, quantity: 1, image:    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAu8AAAH0CAYAAACNesOUAAAgAElEQVR4XoS9iZLjWnIsGMRCAlxy..........,    full_image:    data:image/octet-stream;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBg..........,

If I remove the images then it works fine. And it also works perfectly in localhost. This issue occurs in server only. Here is the link of the website: http://emlt.thebakeologyindia.com/details/1. You can add any other product it will work fine but when you will try to add customize back cover products then this issue occurs because then only I am sending the images. I am not able to figure out what the problem is.

Here is the complete error log:

{
    "message": "The GET method is not supported for this route. Supported methods: POST.",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException",
    "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
    "line": 256,
    "trace": [
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
            "line": 242,
            "function": "methodNotAllowed",
            "class": "Illuminate\\Routing\\RouteCollection",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
            "line": 176,
            "function": "getRouteForMethods",
            "class": "Illuminate\\Routing\\RouteCollection",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 634,
            "function": "match",
            "class": "Illuminate\\Routing\\RouteCollection",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 623,
            "function": "findRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 612,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 176,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 21,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 62,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 57,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 163,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 104,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 151,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/MobileCoverEcom/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 116,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/home/thebvcgn/emlt.site/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}

Upvotes: 0

Views: 1954

Answers (3)

Archit Singh
Archit Singh

Reputation: 212

It is definitely due to the server configuration. Try hosting it in different environment, it will work. I too have faced this issue and I think its due to upload image size php configuration in the server.

Upvotes: 0

bmatovu
bmatovu

Reputation: 4074

Your route specifies POST method.

But you hitting the method via a GET method.

Either correct the method call to POST, or make the route accept both GET and POST

Route::match(['get', 'post'], '/action', 'ExampleController@action');

Upvotes: 2

Fahd Yousri
Fahd Yousri

Reputation: 411

The error states a problem with the route. It indicates that you configured the route to work with get and head verbs yet are using post in the ajax call.

please run: php artisan route:list to check if the route you're using in the ajax call is loaded.

Upvotes: 0

Related Questions