Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不允许Laravel 405方法通过GET调用POST方法_Javascript_Ajax_Laravel_Server - Fatal编程技术网

Javascript 不允许Laravel 405方法通过GET调用POST方法

Javascript 不允许Laravel 405方法通过GET调用POST方法,javascript,ajax,laravel,server,Javascript,Ajax,Laravel,Server,每当我尝试在主体中使用base64图像进行ajax调用时,都会出现如下错误: 此路由不支持GET方法。支持的方法:POST 以下是我的ajax调用: async function addToCart() { document.getElementById('preloader').style.display = 'block'; document.getElementById('addToCartButton').style.display = 'none';

每当我尝试在主体中使用base64图像进行ajax调用时,都会出现如下错误:

此路由不支持GET方法。支持的方法:POST

以下是我的ajax调用:

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",
                });
            }
        });
    }
以下是表单数据示例:

_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..........,
如果我删除了这些图像,那么效果很好。而且它在localhost中也能完美地工作。此问题仅在服务器中发生。 以下是网站的链接:。您可以添加任何其他产品,这将很好地工作,但当您尝试添加自定义封底产品时,就会出现此问题,因为只有我发送图像。我想不出是什么问题

以下是完整的错误日志:

{
    "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": "->"
        }
    ]
}

该错误表示路由有问题。这表示您已将路由配置为使用get和head谓词,但在ajax调用中仍使用post

请运行:
php工匠路线:列表

检查您在ajax调用中使用的路由是否已加载。

您的路由指定POST方法

但是您可以通过GET方法点击该方法

请更正对POST的方法调用,或使路由同时接受GET和POST

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

这肯定是由于服务器配置。尝试在不同的环境中托管它,它会工作的。我也遇到过这个问题,我认为这是由于在服务器上上传了图像大小的php配置。

我用新的错误日志更新了它。当我不在正文中以字符串形式发送base64图像值时,它似乎可以工作。我认为这些图片会导致一些问题,但我无法解决。请您在routes/web.phpRoute::post('add_to_cart','EcommController@addToCart')->中间件('auth');好的,现在路由使用post动词,这就是我们需要的,但是它正在接收get请求,这就是当前显示的错误。问题是,您是否更改了ajax调用中的类型以获取?如果您需要将其更改回post,或者如果您正在尝试使用postman,请确保使用post动词not getNo,我没有更改ajax调用中的方法。它仍然是post。我在路径中使用post方法,即web.php。如果我删除图像URL,那么同样的方法也可以很好地工作。这很奇怪。您可以为该方法豁免
auth
,并尝试通过Postman复制相同的问题,以确保它不是Ajax发送错误的请求。当我与Postman打交道时,我也会遇到相同的问题。我必须重新创建您的场景,以最好地理解它的唯一性。