I trying to make a POST request to Slack API using raw JSON but I keep getting the error below
Access to XMLHttpRequest at 'https://hooks.slack.com/services/conuation/of/the/url' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
Below is code
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");
});
I am using VueJS and Axios HTTP library for the call. Whenever I test with POSTMAN, it works fine.
You can't send this request from frontend to this service. A lot of services block it.
You can create your backend sub service and send request to slack api. So in result you have your own service with url mydomain.com/services/continuation/of/url, when you call it, your service will make call to https://hooks.slack.com/services/continuation/of/url and return slack response