Skip to content

Instantly share code, notes, and snippets.

@liuliqiang
Created August 20, 2018 15:42
Show Gist options
  • Save liuliqiang/ac6c0fff334d408b91f3da28082863cc to your computer and use it in GitHub Desktop.
Save liuliqiang/ac6c0fff334d408b91f3da28082863cc to your computer and use it in GitHub Desktop.
XMLHTTPRequest
<html>
<!-- created by https://liqiang.io -->
<head>
<script type="text/javascript">function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "/ajax/demo_post2.asp", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var body = {
fname: "Bill",
lname: "Gates"
}
xmlhttp.send(JSON.stringify(body));
}</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
<html>
<!-- created by https://liqiang.io -->
<head>
<script type="text/javascript">(function() {
var json = '{"result":true, "count":42}';
obj = JSON.parse(json);
console.log(obj.count);
// expected output: 42
console.log(obj.result);
// expected output: true
})()</script>
</head>
<body></body>
</html>
<html>
<!-- created by https://liqiang.io -->
<head>
<script type="text/javascript">function loadXMLDoc() {
// code for IE7+, Firefox, Chrome, Opera, Safari
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "/ajax/demo_get.asp", true);
xmlhttp.send();
}</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment