Search code examples
javascripthttppostweb-worker

Submit POST Data Without Using DOM


In javascript, I need to send some POST data, but I can't access DOM, which rules out things like making dummy DOM forms, FormData or JQuery.

I've searched and tried, and the best I can come up with is

xhr=new XMLHttpRequest();
xhr.open("POST","//example.org",false);
xhr.send("foo=bar&bah=baz&pipe=%7C")

This works, but the server sends back an error. I'm not sure whether this is a logic error (i.e. I'm not talking to the server right), or whether I'm getting something wrong in the HTTP. Is this the right way to send POST data, and if not, what is?


Solution

  • Seems like adding

    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    

    before sending solves the problem.