if (xhr.readyState === 0) console.log("0: Client has been created. open() not called yet.");
xhr.onreadystatechange = function () { if (this.readyState === 1) console.log("1: open() has been called."); if (this.readyState === 2) console.log( "2: send() has been called, and headers and status are available." ); if (this.readyState === 2) { console.log(this.getAllResponseHeaders(), this.status); } if (this.readyState === 3) console.log("3: Downloading; responseText holds partial data."); if (this.readyState === 4) console.log("4: The operation is complete."); if (this.readyState === 4 && this.status === 200) { myArr = JSON.parse(this.responseText); document.getElementById("demo").innerHTML = myArr; } }; xhr.open("GET", "./json_demo.txt", true); xhr.send();
<body> <divid="divCustomers"></div> <scripttype="text/javascript"> functioncallbackFunction(result, methodName) { var html = "<ul>"; for (var i = 0; i < result.length; i++) { html += "<li>" + result[i] + "</li>"; } html += "</ul>"; document.getElementById("divCustomers").innerHTML = html; } </script> <script type="text/javascript" src="https://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction" ></script> </body> </html>
<!-- https://www.runoob.com/try/ajax/jsonp.php?jsoncallback=callbackFunction respose result as follow callbackFunction(["customername1", "customername2"]) -->