Skip to content

Instantly share code, notes, and snippets.

@Shellbye
Last active October 13, 2016 01:54
Show Gist options
  • Select an option

  • Save Shellbye/477d38cbc1ac86a1f24dc836a3683e9c to your computer and use it in GitHub Desktop.

Select an option

Save Shellbye/477d38cbc1ac86a1f24dc836a3683e9c to your computer and use it in GitHub Desktop.
implement ajax post by pure js
var data = [
{'key': 'username', 'value': 'shellbye'},
{'key': 'password', 'value': 'password1'},
{'key': 'age', 'value': '26'},
{'key': 'key_x', 'value': "value_x"},
{'key': 'key_y', 'value': "value_y"}
];
function js_ajax(url, data) {
var myForm = new FormData();
for (var i = 0; i < data.length; i++) {
myForm.append(data[i].key, data[i].value);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", url);
xmlHttp.send(myForm);
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
console.log("js_ajax_done");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment