functionloadXMLDoc(){ // init a xmlhttprequest const xmlhttp = newXMLHttpRequest();
// init a onreadystatechange and parse xml to json xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var myArr = JSON.parse(this.responseText); myFunction(myArr) } } //send the xml request xmlhttp.open("GET","/try/ajax/json_ajax.json",true); xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); xmlhttp.send(); } // function to change the dom functionmyFunction(arr) { var out = ""; var i; for(i = 0; i < arr.length; i++) { out += '<a href="' + arr[i].url + '">' + arr[i].title + '</a><br>'; } document.getElementById("myDiv").innerHTML=out; }
const p = newPromise((resolve, reject)=>{ const xmlhttp = newXMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4){ if (xmlhttp.status==200) { //success resolve(this.responseText) }else{ //fail reject(xmlhttp.status) } } } })
//promise followup const result = p.then(function(value){ let myArr = JSON.parse(value) myFunction(myArr) return myArr },function(reason){ console.log(reason) return'error' })
// function to change the dom functionmyFunction(arr) { var out = ""; var i; for(i = 0; i < arr.length; i++) { out += '<a href="' + arr[i].url + '">' + arr[i].title + '</a><br>'; } document.getElementById("myDiv").innerHTML=out; }