Advertisement
Guest User

getJSON

a guest
Feb 8th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Weather.prototype.getJSON = function( url, callback ) {
  2.         return new Promise( function( resolve, reject ) {
  3.             // Create a new HTTP request to the url provided
  4.             var request = new XMLHttpRequest();
  5.  
  6.             // The 3rd parameter must be set to true in order to create an asynchronous request.
  7.             request.open( "GET", url, true );
  8.  
  9.             request.onreadystatechange = function() {
  10.                 if ( request.readyState === 4 && request.status === 200) { // 4 is done & 200 is OK
  11.                     // Success!
  12.                     callback( JSON.parse( request.responseText ) );
  13.                 }
  14.             };
  15.  
  16.             request.send();
  17.         } );
  18.  
  19.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement