Skip to content

Instantly share code, notes, and snippets.

@DerexScript
Created October 12, 2021 16:31
Show Gist options
  • Save DerexScript/a8642ff7340cc9cb5e0f26f3b7bacf14 to your computer and use it in GitHub Desktop.
Save DerexScript/a8642ff7340cc9cb5e0f26f3b7bacf14 to your computer and use it in GitHub Desktop.
xhr.js
var form = new FormData;
var xhr = new XmlHttpRequest;
xhr.open('path/to/api', 'POST', true);
xhr.onreadystatechange = function(e) {
console.log(e); // Check success/fail
}
var toSendData = {
proName: [
'Product 1',
'Product 2'
],
proDesc: ['', ''],
amount: [10.0, 15.5],
discount: [0, 0],
total: [10.0, 15.5]
};
for(var i in toSendData) {
form.append(i, toSendData[i]);
}
xhr.send(form);
@DerexScript
Copy link
Author

var form = new FormData(document.forms[0]);
...
xhr.send(form);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment