<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> window.onload = function(){ var oBtn = document.getElementById('btn1'); oBtn.onclick = function(){ //一、聲明一個ajax對象 var xhr = new XMLHttpRequest(); //二、輸入請求的信息 /* 第一個參數:請求的方式 get post 第二個參數:請求的url 第三個參數:是否異步 true (異步) false (同步) */ xhr.open("get", "1.txt", true); //三、發送 xhr.send(); //四、等待數據響應 xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ alert(xhr.responseText); } } } } </script> </head> <body> <button id = 'btn1'>下載數據</button> </body> </html>
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> window.onload = function(){ var oBtn = document.getElementById('btn1'); oBtn.onclick = function(){ //一、聲明一個ajax對象 /* XMLHttpRequest 在低版本的IE瀏覽器下並不兼容 */ var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else{ xhr = new ActiveXObject("Microsoft.XMLHTTP"); //二、輸入請求的信息 /* 第一個參數:請求的方式 get post 第二個參數:請求的url 第三個參數:是否異步 true (異步) false (同步) */ xhr.open("get", "1.txt", true); //三、發送 xhr.send(); //四、等待數據響應 xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ alert(xhr.responseText); } } } } </script> </head> <body> <button id = 'btn1'>下載數據</button> </body> </html>
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> try{ alert("認識成果"); alert("追到手"); alert("娶成果"); throw new Error("追到手代碼執行正常"); }catch(error){ alert("我是補救代碼:" + error); //我是補救代碼:ReferenceError: num is not defined } alert("我是最後的代碼") </script> </head> <body> </body> </html>
好玩吧!
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> window.onload = function(){ var aBtns = document.getElementsByTagName("button"); aBtns[0].onclick = function(){ var xhr = null; try{ xhr = new XMLHttpRequest(); }catch(error){ xhr = new ActiveXObject("Microsoft.XMLHTTP") } var str = "username=tian&age=18&password=123abc"; xhr.open("get", "code14/1.get.php?" + str, true); xhr.send(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ alert(xhr.responseText); }else{ alert("Error:" + xhr.status); } } } } aBtns[1].onclick = function(){ var xhr = null; try{ xhr = new XMLHttpRequest(); }catch(error){ xhr = new ActiveXObject("Microsoft.XMLHTTP") } var str = "username=tian&age=18&password=123abc"; xhr.open("post", "code14/1.post.php", true); //設置提交數據格式 xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); //post提交的數據是經過send方法進行傳參的,注意post提交的數據不加xhr.send(str); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ alert(xhr.responseText); }else{ alert("Error:" + xhr.status) } } } } } </script> </head> <body> <button>GET請求</button> <button>POST請求</button> </body> </html>
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> //傳入對象,拼接成querystring function querystring(obj){ var str = ''; for(var attr in obj){ str += attr + "=" + obj[attr] + "&"; } return str.substring(0, str.length - 1); } //username=tian&age=20&sex=男 alert(querystring({ username: "tian", age: 20, sex: "男" })) </script> </head> <body> </body> </html>
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> /* type 表明 請求方式 url 表明 請求url路徑 data 表明 發送數據 */ function $ajax(type, url, objData){ var xhr = null; try{ xhr = new XMLHttpRequest(); }catch(error){ xhr = new ActiveXObject("Microsoft.XMLHTTP") } if(type == "get" && objData){ url += "?" + querystring(objData); } xhr.open(type, url, true); if(type == "get"){ xhr.send(); }else{ //設置提交數據格式 xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); objData ? xhr.send(querystring(objData)) : xhr.send(); } xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200){ alert(xhr.responseText); }else{ alert("Error:" + xhr.status); } } } } function querystring(obj){ var str = ''; for(var attr in obj){ str += attr + "=" + obj[attr] + "&"; } return str.substring(0, str.length - 1); } window.onload = function(){ var aBtns = document.getElementsByTagName("button"); aBtns[0].onclick = function(){ $ajax("get", "code14/1.get.php", { username: "小明", age: 40, password: "123abc" }); } aBtns[1].onclick = function(){ $ajax("post", "code14/1.post.php", { username: "小花", age: 18, password: "123abc" }); } } </script> </head> <body> <button>GET請求</button> <button>POST請求</button> </body> </html>