Create XMLHttpRequest - Node.js Network

Node.js examples for Network:Ajax

Description

Create XMLHttpRequest

Demo Code


window.get = function(url, success, error) {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        success(xhr.responseText);//from w w w  .  j a va2 s  .  c o  m
      } else {
        error(xhr.responseText);
      }
    }
  };

  xhr.open("GET", url);
  xhr.send();
};

window.getJSON = function(url, success, error) {
  window.get(url,
    function(json) { success(JSON.parse(json)); },
    function(json) {   error(JSON.parse(json)); }
  );
};

Related Tutorials