Click Here For MCQ

Saturday, June 20, 2020

Javascript AJax xmlhttprequest states using code

Javascript AJax state 



let fetchBtn= document.getelementrybyid('fetchBTN');

fetchBTN.addeventlistner('click',loaDoc)
.
//if readystate = 1 mean open 2 mean send 3 mean loading 4 mean done  
function
 loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("demo").innerHTML =
            this.responseText;
       }
    };
    xhttp.open("GET""ajax_info.txt"true);
         xhttp.onprogress =function()
{

}



// if you want data on after on load 


         xhttp.onload function()
{

if (this.readyState == 4 && this.status == 200
{


let obj=JSON.parse(this.responseText);

let list=document.getElementrybyid('divlist');
for(ket in obj)
      {
                 str +='<li>${obj[key].employee_name}</li>';
       }
list.innerhtml= str;

}


}






    xhttp.send();
}





when you sending post request
 xhttp.open("Post""http/123.6.77.6/api/apply"true);
xhhtp.getResponseHeader('Content-Type','Json');

mydata='{name="ritesh", fname="satya", id ="67"}';
xhttp.send(myData);








other program





<script>
$(document).ready(function(){
$('#btn').click(function(){

$('#div1').load("sample.html",function(respomseTxt,statusTxt,xhr)
{
if(statusTxt=="success")

alert("all ok");
if(statusTxt=="error")
alert("Error:"+xhr.status + ":"+ xhr.statusTxt);
});
});
});
</script>



get mathod  

syntax get(url,data,callbackfunction)

$.get("samp.html","id=3",function(responseTxt,statusTxt,xhr){
$('#divid').text(responseTxt)
}).fail(function(){

}).done(function(){

})


post mathod

var data={name:"ritesh",id:"1" , fname:"rahul"}

$.post("samp.html","id=3",function(responseTxt,statusTxt,xhr){
$('#divid').text(responseTxt)
}).fail(function(){

}).done(function(){

})

No comments:

Post a Comment