0

I'm trying to send XML data to a server using JavaScript through a POST request in a chrome extension though being new to JavaScript, I'm struggling. I don't know if it's even possible or not but here's what I have so far (the XML data is in the body of a html file in html format):

var http = new XMLHttpRequest();
var url = "(Server data upload service url)";
var params = encodeURIComponent(window.getElementsByTagName("body")[0].innerHTML);
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);


http.send(params);

Any help is appreciated.

H.Hound
  • 13
  • 1
  • 4

1 Answers1

0

you can try something like this

function post(path, data) {

    var form = document.createElement("form");
    form.setAttribute("method", 'post');
    form.setAttribute("action", path);

    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", 'data');
    hiddenField.setAttribute("value", data);

    form.appendChild(hiddenField);

    document.body.appendChild(form);
    form.submit();
}

original code taken from JavaScript post request like a form submit but modified to your situation.

Community
  • 1
  • 1
Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109