3 Nov 2020

JavaScript XMLHttpRequest - Demo/Example with Free* Joke API.


The XmlHttpRequest object is used to exchange data between Cilient & Server.
XHR fetches web content into you webpage without reloading the page.


What is XMLHttpRequest?

XmlHttpRequest is a predifed Javascript Object whose core functionallty is to send data to server & receive requested data from server. To use XmlHttpRequest, Users should have a JavaScript enabled Browser environment.

Free Joke Api
Read Documentation & See working Examale at
https://sv443.net/

GET Request.

This is an example that How is XmlHttpRequest 'GET' request is made.


const xhttp = new XMLHttpRequest();

xhttp.onload = funtion (){
    
    if(this.status == 200 || xhttp.statusText == "OK"){ // Request Succesful 
    
    var data = this.resonseText; // do any thind with Data
        
    }
    
}

xhttp.open("GET", "https://sv443.net/jokeapi/v2/joke/Any?type=single&amount=10");

xhttp.send();
https://sv443.net/jokeapi/v2/joke/Any?type=single&amount=1

PUT Request

The Request PUT is used to create new data on a server.
See an example that how I have made put request.

        
 var jokedata = { 
 formatVersion: 3,
 category: "Miscellaneous",
 type: "single", 
 joke: "A man was going by bycycle on persion say you wheel is moving", 
 "flags": { 
 nsfw: true, 
 religious: false, 
 political: false, 
 racist: false, 
 sexist: false 
  },
 lang: "en" 
 };
        
 const xhttp = new XMLHttpRequest(); 
            
 xhttp.onload = function (){
 
      if(this.status == 200 || xhttp.statusText == "OK"){ 
      // Request Succesful
      
   var data = this.resonseText; // do any thind with Data 
                 } 
              
             };
             
 xhttp.open("PUT", "https://sv443.net/jokeapi/v2/submit", true ); 
            
 xhttp.send( JSON.Stringify( jokedata ));
   
        

Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE