【问题标题】:CORS policy laravel vueJSCORS 政策 laravel vueJS
【发布时间】:2021-02-13 03:26:46
【问题描述】:

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

我尝试将 index.php 中的标头添加到 Laravel,返回 CORS,我已绑定在 petetition 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 文件

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

];

【问题讨论】:

  • cors 标头必须从服务器返回,您不必使用 axios 在客户端请求中添加它们。如果您使用的是 laravel > 7,则必须在 config/cors.php 中设置正确的标题。 See here
  • @gbalduzzi 感谢您的回复。编辑我的问题,你可以看到我的文件...

标签: php laravel vue.js cors


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-11-15
    • 2021-07-12
    • 2020-07-28
    • 2020-03-30
    • 2021-06-13
    • 1970-01-01
    • 2021-04-27
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多