简体   繁体   English

CORS 政策 laravel vueJS

[英]CORS policy laravel vueJS

I have a problem with CORS in laravel and vueJS.我在 laravel 和 vueJS 中遇到 CORS 问题。 i need send parameter to other website with my aplication in laravel, but always return error CORS.我需要使用我在 Laravel 中的应用程序将参数发送到其他网站,但总是返回错误 CORS。 I tried create a middleware but i don´t know how i implements my routes with this.我尝试创建一个中间件,但我不知道如何用它实现我的路由。

i´m tried add header in index.php to Laravel, returned CORS, i´m tied add this code in petetition axios我尝试将 index.php 中的标头添加到 Laravel,返回 CORS,我绑定在请求 axios 中添加此代码

let url = "/contrarBono30Min";
                    let bono = "Bono30Min";
                    axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
                    axios.post(url, { 
                                bono: bono,
                                token: csrf_token,
                                } 
                            )

My Controller我的控制器

    public function contrarBono30MinHome(Request $request){
    $usuario = \Auth::user()->id;
    $bono = $request["bono"];

    $precioBono = \DB::select("SELECT precio FROM bonos WHERE codBono = '1'");


    return redirect('.....URL?bono='.$bono.'&user='.\Auth::user()->nombre.'&nif='.\Auth::user()->nif.'&precio='.$precioBono[0]->precio);
}

action button动作按钮

contratar: function(){
        $(".contratar").on("click", function(e){

            var bono = $(this).closest("tr").find("td:eq(0)").text();
            var csrf_token = $('meta[name="csrf-token"]').attr('content');

            if(bono == 1){
                let url = "/contrarBono30Min";
                let bono = "Bono30Min";

                axios({
                    method: 'POST',
                    url: url,
                    data: {
                        bono
                    }
                });
            }

            if(bono == 2){
                let url = "/contrarBono1H";
                axios.post(url, {bono:Bono1H})
                        .then((response) => {
                            console.log(response);
                        });
            }

            if(bono == 3){
                let url = "/contrarBono5h";
                axios.post(url, {bono:Bono5H})
                        .then((response) => {
                            console.log(response);
                        });
            }

            if(bono == 4){
                let url = "/contrarBono10H";
                axios.post(url, {bono:Bono10H})
                        .then((response) => {
                            console.log(response);
                        });
            }

            if(bono == 5){
                let url = "/contrarBono24H";
                axios.post(url, {bono:Bono24H})
                        .then((response) => {
                            console.log(response);
                        });
            }

        });

nothing, return CORS...没什么,返回CORS...

my URL in axios is external, i need to send info to other web, how pad type, price, name buyers, etc... But redirect i have a CORS ERROR我在 axios 中的 URL 是外部的,我需要将信息发送到其他网站,如何垫类型、价格、买家姓名等...但重定向我有一个 CORS 错误

i don´t know how resolve this problem.我不知道如何解决这个问题。

thanks for help感谢帮助

CORS FILE CORS 文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];

put these lines inside your public/index.php, this is the easiest way.将这些行放在 public/index.php 中,这是最简单的方法。

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');标头('访问控制允许方法:GET,PUT,POST,DELETE,OPTIONS');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM