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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's doing a standard form post, OP wants to use XMLHttpRequest (ajax) to post the data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.