Skip to content Skip to sidebar Skip to footer

What Is The Vanilla Js Version Of Jquery's $.getjson

I need to build a project to get into a JS bootcamp I am applying for. They tell me I may only use vanilla JS, specifically that frameworks and Jquery are not permitted. Up to th

Solution 1:

Here is the Vanilla JS version for $.getJSON :

var request = newXMLHttpRequest();
request.open('GET', '/my/url', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!var data = JSON.parse(request.responseText);
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send();

Ref: http://youmightnotneedjquery.com/

For JSONP SO already has the answer here

With $.getJSON you can load JSON-encoded data from the server using a GET HTTP request.

Solution 2:

ES6 has Fetch API which provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.

It is easier than XMLHttpRequest.

fetch(url) // Call the fetch function passing the url of the API as a parameter
.then(function() {
    // Your code for handling the data you get from the API
})
.catch(function() {
    // This is where you run code if the server returns any errors
});

Solution 3:

Here is a vanilla JS version of Ajax

var $ajax = (function(){
    var that = {};
    that.send = function(url, options) {
        var on_success = options.onSuccess || function(){},
            on_error   = options.onError   || function(){},
            on_timeout = options.onTimeout || function(){},
            timeout    = options.timeout   || 10000; // msvar xmlhttp = newXMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //console.log('responseText:' + xmlhttp.responseText);try {
                    var data = JSON.parse(xmlhttp.responseText);
                } catch(err) {
                    console.log(err.message + " in " + xmlhttp.responseText);
                    return;
                }
                on_success(data);
            }else{
                if(xmlhttp.readyState == 4){
                    on_error();
                }
            }
        };
        xmlhttp.timeout = timeout;
        xmlhttp.ontimeout = function () { 
            on_timeout();
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }
    return that;
})();

Example:

$ajax.send("someUrl.com", {
    onSuccess: function(data){
        console.log("success",data);
    },
    onError: function(){
        console.log("Error");
    },
    onTimeout: function(){
        console.log("Timeout");
    },
    timeout: 10000
});

Solution 4:

I appreciate the vanilla js equivalent of a $.getJSON above but I come to exactly the same point. I actually was trying of getting rid of jquery which I do not master in any way . What I'm finally strugglin with in BOTH cases is the async nature of the JSON request.

What I'm trying to achieve is to extract a variable from the async call

functionshorten(url){

var request = newXMLHttpRequest();
bitly="http://api.bitly.com/v3/shorten?&apiKey=mykey&login=mylogin&longURL=";
request.open('GET', bitly+url, true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
  var data = JSON.parse(request.responseText).data.url;
  alert ("1:"+data); //alerts fine from within // return data is helpless
  } 
};

request.onerror = function() {
   // There was a connection error of some sortreturn url;
};

request.send();

}

now that the function is defined & works a treat

shorten("anyvalidURL"); // alerts fine from within "1: [bit.ly url]"

but how do I assign the data value (from async call) to be able to use it in my javascript after the function was called

like e.g

document.write("My tiny is : "+data);

Post a Comment for "What Is The Vanilla Js Version Of Jquery's $.getjson"