How to transform AJAX with Jquery into pure Javascript?

Asked

Viewed 434 times

5

How can I do the following AJAX in pure Javascript, without using Jquery?

$.ajax({
  url: "http://habbxo.esy.es/test.html",
  success: function b64EncodeUnicode(entry) {
    nome = entry.split("uniqueId\":\"")[1]["split"]("\"")[0];
  }
});


        setTimeout(function(){alert(nome)},1000);
  • http://youmightnotneedjquery.com/

1 answer

2

native version:

var xmlhttp;
var nome;

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
       if(xmlhttp.status == 200){
           nome = xmlhttp.responseText.split("uniqueId\":\"")[1]["split"]("\"")[0];
       }
       else if(xmlhttp.status == 400) {
          alert('There was an error 400')
       }
       else {
           alert('something else other than 200 was returned')
       }
    }
};

xmlhttp.open("GET", "http://habbxo.esy.es/test.html", true);
xmlhttp.send();

setTimeout(function(){alert(nome)},1000);

Withdrawn response from here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.