相关疑难解决方法(0)

循环中的jQuery.ajax()

如果我jQuery.ajax()在循环内部调用,是否会导致当前迭代中的调用覆盖最后一次调用,或者为新请求分配了新的XHR对象?

我有一个循环来执行此操作,而从控制台日志我可以看到请求已完成,200 ok但只是循环中最后一个请求的结果数据由请求存储success callback为假设.

代码:

var Ajax = {
    pages: {},

    current_request: null,

    prefetch: function () {
        currentPath = location.pathname.substr(1);

        if(this.pages[currentPath])
        {
            var current = this.pages[currentPath];
            delete this.pages[currentPath];

            current['name']=currentPath;
            current['title']=$("title").text().replace(' - '.SITE_NAME, '');
            current['meta_description']=$("meta[name=description]").attr('content');
            current['meta_keywords']=$("meta[name=keywords]").attr('content');          
        }

        var _Ajax = this;
        //the loop in question *****
        for(var key in this.pages)
        {
            $.ajax({
                method: 'get',
                url:'http://'+location.hostname+'/'+key,
                success: function(data) {
                    _Ajax.pages[key] = data;    
                }
            }); 

                    console.debug(this.pages);
        }

        if(current)
        {
            this.pages[currentPath] = current;
        }       

    } 
};//Ajax Obj
for(var …
Run Code Online (Sandbox Code Playgroud)

ajax jquery xmlhttprequest

9
推荐指数
4
解决办法
3万
查看次数

标签 统计

ajax ×1

jquery ×1

xmlhttprequest ×1