Last active
October 13, 2016 01:54
-
-
Save Shellbye/477d38cbc1ac86a1f24dc836a3683e9c to your computer and use it in GitHub Desktop.
implement ajax post by pure js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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