I'm trying to send data to a server through my frontend. I have the following code,
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Access-Control-Allow-Origin','*');
xhr.setRequestHeader('Content-type','text/plain');
xhr.setRequestHeader('Access-Control-Allow-Methods','POST');
xhr.send(data);
The data
is a JSON string which should be sent to the server. However, I get the following response on the server "[1m[35mOPTIONS /addUser HTTP/1.1[0m" 500 -
. It's not clear why the request is being sent as OPTIONS
when Content-type
is set as text/plain
i.e. the request should not be pre-flighted.
How can I send data to the server using CORS?