如何在VueJS上对所有请求标头字段进行access-control-allow-origin

问题描述 投票:0回答:1

[我试图使用原始JSON向Slack API发出POST请求,但我不断收到以下错误

从原点[https://hooks.slack.com/services/conuation/of/the/url]从'http://localhost:8080'对XMLHttpRequest的访问已被CORS策略阻止:在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段access-control-allow-origin。

下面是代码

const params =       {
                    "attachments":[
                      {
                        "fallback":"New Project Lead:<" + this.our_website + "|Click here to view>",
                        "pretext":"New Project Lead:<" + this.our_website + "|Click here to view>",
                        "color":"#D00000",
                        "fields":[
                          {
                            "title":"Company",
                            "value":this.user_company,
                            "short":false
                          },
                          {
                            "title":"Country",
                            "value":getName(this.user_country),
                            "short":false
                          }

                        ]
                      }
                    ]
                  };


                    this.axios.post('https://hooks.slack.com/services/continuation/of/url', params, {
                      headers: {
                        'content-type': 'application/json',
                        'Access-Control-Allow-Origin' : '*',
                      },
                    }).then((response)=>{
                        loader.hide();
                        let msg = "Sent Successfully";
                        this.displayAlert("Done", msg, "success");
                        setTimeout(() => {  // redirect to home after 2s
                            document.location = '/';
                        }, 2000);
                    }).catch((error) =>{
                        alert(error);
                    });
                }).catch((error) => {
                    loader.hide();
                    let msg = "Error : " + error;
                    this.displayAlert("Error", msg, "error");
                });

我正在使用VueJS和Axios HTTP库进行通话。每当我用POSTMAN测试时,它都可以正常工作。

javascript http vue.js axios vue-cli-3
1个回答
0
投票

您无法从前端发送此请求。

您可以创建您的后端子服务并将请求发送到Slack API这样就可以了

© www.soinside.com 2019 - 2024. All rights reserved.