0

我正在尝试制作一个 AJAX JavaScript 类,代码如下:

// Create a new User object that accept an object of properties
function JsonLoader() {

    function ajaxRequest() {
        var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
        if (window.ActiveXObject) { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
            for (var i=0; i<activexmodes.length; i++) {
                try {
                    return new ActiveXObject(activexmodes[i]);
                }
                catch(e) {
                    //suppress error
                }
            }
        }
        else if (window.XMLHttpRequest) // if Mozilla, Safari etc
            return new XMLHttpRequest();
        else
            return false;
    }

    this.loadJosonData = function() {
        var mygetrequest = ajaxRequest();

        mygetrequest.onreadystatechange = function() {
            if (mygetrequest.readyState==4) {
                if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1) {
                    var jsondata=eval("("+mygetrequest.responseText+")"); //retrieve result as an JavaScript object
                } else {
                    alert("An error has occured making the request");
                }
            }
        }

        mygetrequest.open("GET", 'some_url', true);
        mygetrequest.send(null);
    };
}

现在我想使用从 AJAX 调用返回的 jsondata 值,返回 jsondata 变量的最佳方法是什么?

4

0 回答 0