<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> <script> //手寫ajax let xhr = new XMLHttpRequest; xhr.open("POST", "http://jspang.com/DemoApi/oftenGoods.php", true); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { console.log(xhr.responseText) } } xhr.send() </script> </html> //================================================ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> <script> //手寫ajax var xhr = new XMLHttpRequest() xhr.open('POST', "http://jspang.com/DemoApi/oftenGoods.php", true) // false就是等待有返回數據的時候再繼續往下走,尚未獲得數據的時候就會卡在那裏,直到獲取數據爲止。 // true就是不等待,直接返回,這就是所謂的異步獲取數據! xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status == 200) { console.log(xhr.responseText) } } } xhr.send() </script> </html>