AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
<head> <script> function loadXMLDoc() { .... AJAX 脚本执行 ... } </script> </head>
步骤:
//1. 创建核心对象 var xhttp; if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //2. 发送请求 xhttp.open("GET", "http://localhost:8080/ajax-demo/ajaxServlet"); xhttp.send(); //3. 获取响应 xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { alert(this.responseText); } };
Axios是对AJAX的一个封装,在使用之前需要声明需要导入的js文件
<script src="js/axios-0.18.0.js"></script>
使用步骤:
<script> //1. get /* axios({ method:"get", url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan" }).then(function (resp) { alert(resp.data); })*/ //2. post axios({ method:"post", url:"http://localhost:8080/ajax-demo/axiosServlet", data:"username=zhangsan" }).then(function (resp) { alert(resp.data); }) </script>
或者使用别名的方式:
axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan").then(function (resp) { alert(resp.data); }) axios.post("http://localhost:8080/ajax-demo/axiosServlet","username=zhangsan").then(function (resp) { alert(resp.data); })
标签:function,Axios,http,demo,resp,xhttp,AJAX,&&,localhost From: https://www.cnblogs.com/zhaolei0419/p/16720987.html