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.
Access-Control-Allow-Originheader to the server it isn't going to be used. These headers are sent from the server to tell the browser if it is allowed to access the resources at that endpoint. If you are having CORS issues have a look at the many CORS Q&As on SO