在页面上使用两个xmlhtt prequest电话 [英] Using two xmlhttprequest calls on a page

查看:116
本文介绍了在页面上使用两个xmlhtt prequest电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个分区,< D​​IV ID = statuslist>< / DIV>< D​​IV ID = customerlist>< / DIV>

功能 sendReq()创建一个xmlhtt prequest和获取数据挺进师。

The function sendReq() creates a xmlhttprequest and fetches the data into the division.

sendReq('statuslist','./include/util.php?do=getstatuslist','NULL');

sendReq('customerlist','emphome.php?do=getcustomerlist','NULL');

我有一个问题,

I have a problem,

取到customerlist的数据被复制到statuslist

The data fetched into the 'customerlist' gets copied onto 'statuslist'

如果我改变函数调用的顺序,

If i change the order of function calls,

sendReq('customerlist','emphome.php?do=getcustomerlist','NULL');

sendReq('statuslist','./include/util.php?do=getstatuslist','NULL');

现在的statuslist数据进入customerlist。

Now the data of 'statuslist' gets into 'customerlist'..

请告诉我用code中的问题?

Whats the problem with the code?

推荐答案

在事实上,它可以运行多个异步XHR呼叫,但你必须给他们一个唯一的ID作为参数,能够存储和本地加载它们在你的DOM。

In fact it is possible to run multiple async xhr call but you have to give them an unique id as parameter to be able to store and load them locally in your DOM.

例如,你想循环阵列上并进行Ajax调用为每个对象。这是一个有点棘手,但这个code对我的作品。

For example, you'd like to loop on an array and make a ajax call for each object. It's a little bit tricky but this code works for me.

var xhrarray={};
for (var j=0; j<itemsvals.length; j++){
                var labelval=itemsvals[j];
                // call ajax list if present.
                if(typeof labelval.mkdajaxlink != 'undefined'){
                    var divlabelvalue = '<div id="' + labelval.mkdid + '_' +          item.mkdcck + '" class="mkditemvalue col-xs-12 ' + labelval.mkdclass + '"><div class="mkdlabel">' + labelval.mkdlabel + ' :</div><div id="'+ j +'_link_'+ labelval.mkdid +'" class="mkdvalue">'+labelval.mkdvalue+'</div></div>';
                    mkdwrapper.find('#' + item.mkdcck + ' .mkdinstadivbody').append(divlabelvalue);

                    xhrarray['xhr_'+item.mkdcck] = new XMLHttpRequest();
                    xhrarray['xhr_'+item.mkdcck].uniqueid=''+ j +'_link_'+ labelval.mkdid +'';
                    console.log(xhrarray['xhr_'+item.mkdcck].uniqueid);
                    xhrarray['xhr_'+item.mkdcck].open('POST', labelval.mkdajaxlink);
                    xhrarray['xhr_'+item.mkdcck].send();
                    console.log('data sent');
                    xhrarray['xhr_'+item.mkdcck].onreadystatechange=function() {
                        if (this.readyState == 4) {
                            console.log(''+this.uniqueid);
                            document.getElementById(''+this.uniqueid).innerHTML = this.responseText;
                        }
                    };
                }
}

您必须设置每个XHR对象在全局变量对象,并定义一个值 xhrarray ['XHR _'+ item.mkdcck] .uniqueid 得到其唯一的ID,并加载它,你想要的结果。

You have to set each xhr object in a global variable object and define a value xhrarray['xhr_'+item.mkdcck].uniqueid to get its unique id and load its result where you want.

希望这将有助于你在未来。

Hope that will help you in the future.

这篇关于在页面上使用两个xmlhtt prequest电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆