Monday, 8 August 2022

JSON XMLHTTPRequest

 

index.html

<!DOCTYPE html>
<html lang="en">
<body>
<p id="p1"></p>
<button id='bt1'>click me</button>
<ul>
</ul>
<script src="app.js">
</script>
</body>
</html>

aa.json file

{
    "id":1,
    "name":"abc"
}


app.js file

document.getElementById('bt1')
.addEventListener('click',loadData);
function loadData(){
const xhr=new XMLHttpRequest();
xhr.open('GET','aa.json',true);
xhr.onload=function(){
if(this.status===200){
const customers=JSON.parse(this.responseText);          
let output=
`<li>id:${customers.id}</li>
<li>name:${customers.name}</li>`;
document.getElementById('p1').innerHTML=output;
}
}
xhr.send();
}

No comments:

Post a Comment

Regular Expressions In JS

 index.html file <! DOCTYPE html > < html lang = "en" > < body > < script > let re ; re = /hello/ i ;  ...