Create object from remote JSON

Published earlier, but I think I was too specific to get much interest in the Original . I am new to javascript and AJAX and it is hard for me to find a simple answer to this question. My goal is to simply parse the remote JSON by providing me with an object that I can then use in the rest of the application.

The problem I ran into is to keep this object alive (retrieving data using XMLHttpRequest is populated only during the callback function). I have jQuery if this makes the answer easier. Javascript is part of the Chrome extension and has remote JSON permissions.

Hope someone can help

Change code from original post + Scott M answer

var createdDate;

function checkDomainRegistrationLength(url) {
  var serverName = urlToServerName(url);

  var xhr = new XMLHttpRequest();

  xhr.open("GET", "eg.json", false); //Code now working, was set to async
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      var resp = JSON.parse(xhr.responseText);
      createdDate = resp.WhoisRecord.audit.createdDate;
    }
  }
  xhr.send();
  return createdDate;
}
+3
1

. .


edit: jQuery, . $. Ajax(), . ( !) .

, jQuery, .

0

Source: https://habr.com/ru/post/1791696/


All Articles