1

我已经设置了一个脚本来调用我的服务器并返回一个 JSON 字符串。调用的目的是返回登录到我的聊天站点的用户数。但是,在服务器发回数据和我的 javascript 接受数据之间的某个地方,数据被修改了。我使用以下逻辑缩小了范围:

  1. Chrome 和 Firefox 都可以完美运行
  2. Internet Explorer 没有。
  3. 每一步都有断点的开发工具显示返回的数据在脚本开始时已经不正确。
  4. 从逻辑上讲,我前往我的 php 脚本。我直接从 IE 调用它,它回显了正确的数据。

预期数据:

{"BestOfLife":0,"Faith":0,"BookLovers":0,"Leather":0,"Debate":0,"Sheets":0,"TheRoom":0,"TheOtherRoom":0,"GayDudes":0,"Religion":0,"Brains":0,"Flames":0,"Arrow":0,"Bow":0,"Main":3}

返回数据:

{"BestOfLife":0,"Faith":0,"BookLovers":0,"Leather":0,"Debate":0,"Sheets":0,"TheRoom":0,"TheOtherRoom":0,"GayDudes":0,"Religion":0,"Brains":0,"Flames":0,"Arrow":0,"Bow":0,"Main":2}

注意最后一个键持有不同的数字。在大多数机器上(在全国范围内测试),这始终返回 2。在我的,有时,它返回三个。这些数字应该经常变化(基于用户数量)。

这是javascript:

var userCount = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
var userCount;  

if(window.ActiveXObject){
    try{
        userCount = new ActiveXObject("Microsoft.XMLHTTP"); 
    }catch(e){
        userCount = false;
    }
}else{
    try{
        userCount = new XMLHttpRequest();
    }catch(e){
        userCount = false;
    }
}

if(!userCount){
    console.log("Can't Show User Count");
}else{
    return userCount;   
}
}

function process(){
if(userCount.readyState==0 || userCount.readyState==4){
    serverCall = "";

    userCount.open("GET", "/src/pfcusercount.php?serverCall=" + serverCall, true);

    userCount.onreadystatechange = handleServerResponse;

    userCount.send(null);

}else{
    setTimeout('process()', 1000);
}
}

function handleServerResponse(){
if(userCount.readyState==4){
    if(userCount.status==200){
            xmlResponse = userCount.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;
            jsonString = xmlDocumentElement.firstChild.data;

            var roomArray = eval('(' + jsonString + ')');

            //set All the room counts

            if(roomArray['Main'] == 1){

                document.getElementById("userCount").innerHTML = '<span style="color:red">You are the only user online!</span>';

            }else{

                document.getElementById("userCount").innerHTML = '<span style="color:red">There are ' + roomArray['Main'] + ' users online!</span>';
            }

            document.getElementById("BestOfLifeC").innerHTML = '<span style="color:red">(' + roomArray['BestOfLife'] + ')</span> BestofLife'; 
            document.getElementById("FaithC").innerHTML = '<span style="color:red">(' + roomArray['Faith'] + ')</span> Faith';
            document.getElementById("BookLoversC").innerHTML = '<span style="color:red">(' + roomArray['BookLovers'] + ')</span> BookLovers';
            document.getElementById("LeatherC").innerHTML = '<span style="color:red">(' + roomArray['Leather'] + ')</span> Leather';
            document.getElementById("DebateC").innerHTML = '<span style="color:red">(' + roomArray['Debate'] + ')</span> Debate';
            document.getElementById("SheetsC").innerHTML = '<span style="color:red">(' + roomArray['Sheets'] + ')</span> Sheets';
            document.getElementById("TheRoomC").innerHTML = '<span style="color:red">(' + roomArray['TheRoom'] + ')</span> TheRoom';
            document.getElementById("TheOtherRoomC").innerHTML = '<span style="color:red">(' + roomArray['TheOtherRoom'] + ')</span> TheOtherRoom';
            document.getElementById("GayDudesC").innerHTML = '<span style="color:red">(' + roomArray['GayDudes'] + ')</span> GayDudes';
            document.getElementById("ReligionC").innerHTML = '<span style="color:red">(' + roomArray['Religion'] + ')</span> Religion';
            document.getElementById("BrainsC").innerHTML = '<span style="color:red">(' + roomArray['Brains'] + ')</span> Brains';
            document.getElementById("FlamesC").innerHTML = '<span style="color:red">(' + roomArray['Flames'] + ')</span> Flames';
            document.getElementById("ArrowC").innerHTML = '<span style="color:red">(' + roomArray['Arrow'] + ')</span> Arrow';
            document.getElementById("BowC").innerHTML = '<span style="color:red">(' + roomArray['Bow'] + ')</span> Bow';

            setTimeout('process()', 1000);

    }else{
        console.log("Unable to show user counts!");
    }
}
}

这是 PHP 脚本的链接,它将根据需要为您回显数据:

http://www.lgbts-chat.com/src/pfcusercount.php

请帮助 :) 随意创建一个用户帐户用于测试目的。目前,注册不进行电子邮件验证,以使其易于调试。如果需要,输入一个假的。

4

1 回答 1

2

好的,我终于想通了。问题最终成为 Internet Explorer(相当烦人的)缓存 ajax 的倾向。当我退出浏览器,重新打开聊天站点并重新登录时,我注意到了这一点,用户计数将始终为总计数 - 1(因为它还没有考虑到我 [新用户])。从那时起,返回的字符串总是相同的。我的解决方案是修改我的电话:

userCount.open("GET", "/src/pfcusercount.php?serverCall=" + serverCall, true);

由于我只是使用 serverCall 作为占位符以防将来需要,因此我在其上方添加了这一行:

serverCall = Math.random();

这诱使 Internet Explorer 认为每个 AJAX 都是完全不同的实体。故事的寓意:IE 的死亡。

于 2013-03-03T06:25:03.570 回答