从远程JSON创建对象

Have posted before but I think I was a little too specific to gain much interest Original. I am new to javascript and AJAX and have found it difficult to find a simple answer to this question. My goal is to simply parse some remote JSON, giving me an object which I can then use in the rest of application.

The problem I am having is keeping this object alive (getting data using XMLHttpRequest it is only populated during the callback function). I have jQuery available if this eases the answer. The javascript is part of a google chrome extention and does have the right permissions to access the remote JSON.

Hope someone can help

Edit; code from original post + Scott M's 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;
}

assign the returned object to a global variable. You can then access it throughout the application.


edit: if you have jQuery available, it makes your life infinitely easier. Use the $.ajax() function to do the work for you. in the callback (which is only called once!) you can then set the variable.

sorry for not seeing the jquery reference earlier, or i would have included this in the answer.