Skip to content

Instantly share code, notes, and snippets.

@liuliqiang
Created August 20, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuliqiang/0115522bba686bc6d646c5024150d1cc to your computer and use it in GitHub Desktop.
Save liuliqiang/0115522bba686bc6d646c5024150d1cc 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