0

That's my code. I don't know what's the problem. I've tried by adding the callback parameter but it doesn't works. Thanks in advance

var json={
getJSON:function(url, successHandler, errorHandler) {
  var xhr = typeof XMLHttpRequest != 'undefined'
    ? new XMLHttpRequest()
    : new ActiveXObject('Microsoft.XMLHTTP');
  xhr.open('get', url, true);
  xhr.onreadystatechange = function() {
    var status;
    var data;
    xhr.onload = function() {
    if (xhr.readyState == 4) { // `DONE`
      status = xhr.status;
      if (status == 200) {
        data = JSON.parse(xhr.responseText);
        successHandler && successHandler(data);
      } else {
        errorHandler && errorHandler(status);
      }
    }}
  };
  xhr.send();
}};

json.getJSON('http://www.exampledomain.com/call.php?test='+example, function(jsonData){
  return jsonData.test; // this doesn't work.
});
  • This doesn't answer your question, but I would recommend using a library like jQuery because they've already implemented a cross-browser getJson() function that works, is tested, and is mature. http://api.jquery.com/jquery.getjson/ – Samuel Aug 13 '15 at 18:58
  • What you are trying to do isn't possible. – Kevin B Aug 13 '15 at 19:02

0 Answers0