<!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>