-1

I'm trying to make a XMLHttpRequest to a server, the server accepts JSON as arguments and it's supposed to register the arguments I'm sending. Here's my code:

function makeXMLRequest() {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "http://mywebserverhere/register", true);
    xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
    xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "POST, GET");
    xmlhttp.setRequestHeader("Content-type", "application/json");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.status != 200)  {
            // Error
        return;
        }
    alert(xmlhttp.responseText);
    }
    xmlhttp.send({"name": "pedro21", "pass": "nAosei1"});
}

But I get this error:

XMLHttpRequest cannot load http://mywebserverhere/register. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.

Any ideas?

Pedro Barros
  • 183
  • 1
  • 11

1 Answers1

0

To allow CORS in a POST request you need to make a header. With GET you don't must to put this, but with POST you need:

 xmlhttp.setRequestHeader('X-PINGOTHER', 'pingpong');
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69