如何从函数内部获取变量(在Ajax调用中使用)

时间:2011-11-20 22:07:56

标签: javascript ajax

问候我有一个ajax调用:

  1. 获取帖子的xml文件
  2. 填充二维数组的帖子信息(文字和链接)
  3. 我接下来需要的是获取数组以执行后续步骤,但我无法实现。

    范围问题? Ajax问题?

    请告知。

    // create the link
    var posts_href = 'get-posts.php';
    
    // Create XHR Object
    function getHTTPObject(){
        var xhr = false;
        // test if standards based
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            try{
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e){
                    xhr = false;
                } 
            }
        }
        return xhr;
    }
    
    // Grab File ASYCHRONOUSLY
    function grabFile(file){
        // create a new instance of the xhr object
        var request = getHTTPObject();
    
        // if successful, initiate ajax request
        if(request){
            request.onreadystatechange = function(){
                parseResponseXML(request);
    
            };
    
            request.open("GET",file,true);
            request.send(null);
            return true;
        } else {
            return false;
        }
    }
    
    // make the ajax call
    grabFile(posts_href);
    
    // create the responseXML function
    function parseResponseXML(req){
        if(req.readyState == 4){
            if(req.status == 200 || req.status == 304){
                var textArray = [];
                var posts = req.responseXML.getElementsByTagName('post');
    
                // create array to hold the posts (strings)     
                textArray = new Array(posts.length);
    
                for(var i=0;i<posts.length;i++){
    
                    var post_text, text, post_link, details_link;
    
                    // get the post info
                    post_text = posts[i].getElementsByTagName('text');
                    text = post_text[0].firstChild.nodeValue
    
                    post_link = posts[i].getElementsByTagName('details_link');
                    if(post_link[0].firstChild != null){
                        details_link = post_link[0].firstChild.nodeValue;
                    } else {
                        details_link = "";
                    }
    
                    // add post to textArray
                    textArray[i] = new Array(text, details_link);
                    // console.log(textArray[i]);
    
                }   // end for..loop                
            }       // end if(req.status)
        }           // end if(req.readyState)   
    }               // end parseResponseXML()
    
    
    // get the value of textArray from the function ->THIS FAILS
    console.log(textArray);
    

1 个答案:

答案 0 :(得分:2)

是的,这是一个ajax问题。 Ajax是异步的。无论在textArray中依赖parseResponseXML的“后续步骤”都需要在回调中完成您可以修改function parseResponseXML(req, callback) { // same as original implementation above // then, callback(textArray); } 以接收应该将结果数组传递给的任意回调:

function doAfterParse(textArray) {
    console.log(textArray);
}

// snip...
request.onreadystatechange = function(){
    parseResponseXML(request, doAfterParse);
};

然后你会用这样的东西:

{{1}}