当前位置: 首页 > 知识库问答 >
问题:

如何在VueJS上允许请求头字段access-control-allow-origin

姬昀
2023-03-14

我试图使用原始JSON发出一个POST请求来松弛API,但我一直得到以下错误

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

共有1个答案

杨建章
2023-03-14

您不能将此请求从前端发送到此服务。很多服务阻止了它。

您可以创建后端子服务并向slack API发送请求。因此,结果是您拥有自己的服务,其url为mydomain.com/services/continuation/of/url,当您调用它时,您的服务将调用https://hooks.slack.com/services/continuation/of/url,并返回slack响应

 类似资料: