Asynchronous Javascript And XML
ajax异步的xml和JavaScript,是一种综合技术利用XMLHttpRequest和服务器进行数据交换通过js动态的渲染页面实现网页异步局部更新输出:A C B
新建jq.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="./js/jquery-3.4.1.js"></script> </head> <body> <div id="app"></div> <button>加载</button> </body> <script> $(function(){ $("button").click(function(){ $("#app").load("be.html .my") //加载be.html中的内容到div中,第二个参数可以指定加载的内容 }) }) </script> </html> $.getScript() 加载.js文件 执行js 新建 text.js alert("你好js")新建jq.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="./js/jquery-3.4.1.js"></script> </head> <body> <div id="app"></div> <button>加载</button> </body> <script> $(function(){ $("button").click(function(){ $.getScript("./test.js") }) }) }) </script> </html> $.getJSON() 新建be.json文件 {"name":"hh","age":"20"}新建jq.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src="./js/jquery-3.4.1.js"></script> </head> <body> <div id="app"></div> <button>加载</button> </body> <script> $(function(){ $("button").click(function(){ // $.getJSON("./be.json",function(res,status,xhr){ // console.log(res,status,xhr) // }) // $.getJSON("./be.json") // .then(res=>console.log(res)) // .then(err=>console.log(err)) $.getJSON("./be.json") .done(res=>{ console.log(res) }) .fail(xhr=>console.log("失败",xhr)) .always(res=>{ console.log("无论成功失败都执行",res) }) }) }) </script> </html> $.get <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/jquery-3.4.1.js"></script> </head> <body> <button type="button">加载</button> <p id="p"></p> </body> <script> //单击加载将be.text的内容放入p标签中 $(function(){ $("button").click(function(){ $.get("./be.text") .then(res=>{$("#p").text(res)}) }) }) </script> </html> $.post <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/jquery-3.4.1.js"></script> </head> <body> <button type="button">加载</button> <p id="p"></p> </body> <script> $(function(){ $("button").click(function(){ $.post( "http://www.xxxx.com/ajax/echo.php", {name:"hhh",age:20}, // function(res,status,xhr){ // console.log(res,status,xhr) // } ) // .then(res=>{ // $("#p").text(res) // }) // .catch(err=>{console.log(err)}) .done(res=>{$("#p").text(res)}) .fail(err=>{console.log(err)}) .always(res=>{console.log(res)}) }) }) </script> </html> $.ajax <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/jquery-3.4.1.js"></script> </head> <body> <button type="button">加载</button> <p id="p"></p> </body> <script> //单击加载将be.text的内容放入p标签中 $(function(){ $("button").click(function(){ // $.ajax({ // url:"be.text", // type:"GET", // success:function(res){ // console.log(res) // }, // fail:function(err){ // console.log(err) // } // }) $.ajax({ url:"http://www.520mg.com/ajax/echo.php", type:"POST", data:{name:"hh",age:18}, success:function(res){ console.log(res) }, fail:function(err){ console.log(err) }, beforeSend(arg){console.log("开始加载提示")}, completa(arg){console.log("关闭加载")} }) .then(res=>{console.log(res)}) .catch(err=>{console.log(err)}) // $.ajax({ // url:"https://wis.qq.com/weather/common?weather_type=observe|forecast_24h|air&source=pc", // dataType:"jsonp", // beforeSend(arg){console.log("开始加载提示")} // completa(arg){console.log("关闭加载")} // }) // .then(res=>{ // console.log(res) // }) // .catch(err=>{ // console.log(err) // }) }) // $(document).ajaxStart(function(e){ // console.log("全局开始") // }) // $(document).ajaxStop(function(e){ // console.log("全局结束") // }) }) </script> </html> fetch() <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <button type="button" id="btn">加载</button> </body> <script> // fetch 通过http管道方式访问服务器 btn.onclick=function(){ // fetch("./be.text") // .then(res=>res.text()) // .then(res=>{console.log(res)}) // .catch(err=>{console.log(err)}) // fetch("./be.json") // .then(res=>res.json()) // .then(res=>{console.log(res)}) // .catch(err=>{console.log(err)}) fetch("http://www.xxx.com/ajax/echo.php",{ method:"POST", body:"name=nu&age=18", headers:{'Content-Type':'application/x-www-form-urlencoded'} }) .then(res=>res.text()) //使用text还是jsonp取决于传过来的数据 .then(res=>{ console.log(res) }) } </script> </html>