CORS政策laravel vueJS

时间:2020-10-30 16:27:41

标签: php laravel vue.js cors

我在laravel和vueJS中遇到CORS问题。我需要在laravel中将参数发送到其他网站,但总是返回错误CORS。我尝试创建中间件,但是我不知道该如何实现我的路由。

我尝试将index.php中的标头添加到Laravel,返回CORS,我将这段代码添加到Petitition axios中

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

我的控制器

    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);
}

操作按钮

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);
                        });
            }

        });

一无所有,返回CORS ...

我在axios中的URL是外部的,我需要将信息发送到其他网站,垫的类型,价格,买家名称等如何。但是重定向时我遇到了CORS错误

我不知道如何解决这个问题。

感谢帮助

CORS FILE

<?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,

];

1 个答案:

答案 0 :(得分:-1)

将这些行放入您的public / index.php中,这是最简单的方法。

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS');