Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
此路由不支持POST方法。支持的方法:GET,HEAD,如何解决此错误?Im使用vue js和laravel 7_Laravel - Fatal编程技术网

此路由不支持POST方法。支持的方法:GET,HEAD,如何解决此错误?Im使用vue js和laravel 7

此路由不支持POST方法。支持的方法:GET,HEAD,如何解决此错误?Im使用vue js和laravel 7,laravel,Laravel,这是我的vue组件 export default { data(){ return{ form:new Form({ district: '', province: '', name: '', }) } }, methods: { createHousehold(){

这是我的vue组件

export default {
    data(){
        return{
            form:new Form({
                district: '',
                province: '',
                name: '',
            })
        }
    },
    methods: {
        createHousehold(){
            this.form.post('api/household');

        }
    },
    mounted() {
        console.log('Component mounted.')
    }
}
这是api路由

Route::apiResource('household','APIController/HouseholdController');

当我检查路由列表“api/Househouse”时,有get用于索引和post用于存储,但我得到了此错误,并且无法确定除了在vue方法中指定post方法之外,在何处指定post方法。我还尝试在主刀片的头部添加csrf解释 您需要设置Access Control Allow Methods标头,指定在响应飞行前请求访问资源时允许的方法

参考URL-

您可以使用以下包(在laravel或PHP中)在服务器端添加cors包

URL-

作曲者需要barryvdh/laravel cors

app/Http/Kernel.php文件下添加中间件组

protected $middlewareGroups = [
    ...
    'api' => [
        'throttle:60,1',
        'bindings',
        \Barryvdh\Cors\HandleCors::class,
    ],
    ...
]
并运行以下命令

php artisan vendor:publish --tag="cors"
使用以下内容更新配置(config/cors.php


尝试使用PHP的header函数设置值,有时您需要重新启动项目和服务器以更改效果。与您相比,我使用fruitcake/laravel cors解决了此问题,并更新了allow方法。我希望您遵循ref.链接,因为此链接还指定了“fruitcake/laravel cors”包。
return [
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedOriginsPatterns' => [],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'], // this is the solution of your problem
    'exposedHeaders' => [],
    'maxAge' => 0,
]