getJSON返回不起作用

时间:2015-08-13 18:56:27

标签: javascript ajax json

这是我的代码。我不知道是什么问题。我已经尝试添加回调参数,但它不起作用。提前致谢

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.
});

0 个答案:

没有答案
相关问题