AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
AJAX 不是新的编程语言,而是一种使用现有标准的新方法。
AJAX 最大的优势是在不从新加载整个页面的状况下,能够与服务器交换数据并更新部分网页内容。
AJAX 不须要任何浏览器插件,但须要用户容许JavaScript在浏览器上执行。javascript
首先要在JQuery使用ajax须要引入jquery对象
此处引入了java
<script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
此处写出一些经常使用的在线引入JS文件的地址jquery
***一、官网jquery压缩版引用地址*** (3.1.1版本:) <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> 3.0.0版本: <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> 2.1.4版本 <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> ***2.百度压缩版引用地址:*** <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> 3.微软压缩版引用地址: <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"></script>
注意:不要一味的追求新版本,不是版本越高就越好用,正如jquery-2.0以上版本再也不支持IE 6/7/8)ajax
$("#xk").click(function(){ $.ajax({ url:'https://baidu.com',//请求的url地址 data:{"name":"张三","tel":1318271717},//请求时携带的参数 type:"GET",//请求方式 POST请求 GET请求 async:true,//是否异步请求,默认是true异步的.这是ajax的特性 //返回的格式也是接受返回的数据格式 //此处注意,你接受的格式要和后端返回的格式一致.不然始终走error dataType:"text", success:function(result){//请求成功处理 console.log(result) }, error:function(er){//请求失败处理 console.log(JSON.stringify(er)) }, complete:function () { //请求完成处理 } }) });
document.getElementById("xke").onclick=function(){ var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET","https://baidu.com",true); xmlHttp.send(); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4&&xmlHttp.status==200){ console.log(xmlHttp.responseText) } } }
于2020-03-28 更新编程
function ajax() { var formData = new FormData(); formData.append("id","001"); formData.append("key","value"); let xml = new XMLHttpRequest(); xml.open("POST","https://baidu.com",true); xml.setRequestHeader("Content-type","application/json") xml.onload=function(){ let result = JSON.parse(xml.responseText); if(result.code==200){ } } xml.send(formData); }