Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript从XML中提取数据(如果else语句不起作用)_Javascript_Html_Ajax - Fatal编程技术网

JavaScript从XML中提取数据(如果else语句不起作用)

JavaScript从XML中提取数据(如果else语句不起作用),javascript,html,ajax,Javascript,Html,Ajax,因此,我在下面代码中的问题在于下面底部的if-else-if语句: 1.两个if语句中的代码都工作得很好。 2.问题是,当我在一台计算机上运行代码时,可以使用它。 如果我做了两个独立的if语句,只有第二个有效。 如果我执行1 if和1 else,则只有if语句有效,而else if不起作用 还有一点信息:我想做的是,每次函数超时并再次循环时,它都会检查if语句,以及是否有更改以运行适当的if子句。 如果需要更多信息,请告诉我 var xmlHttp = createXmlHttpRequestO

因此,我在下面代码中的问题在于下面底部的if-else-if语句: 1.两个if语句中的代码都工作得很好。 2.问题是,当我在一台计算机上运行代码时,可以使用它。 如果我做了两个独立的if语句,只有第二个有效。 如果我执行1 if和1 else,则只有if语句有效,而else if不起作用

还有一点信息:我想做的是,每次函数超时并再次循环时,它都会检查if语句,以及是否有更改以运行适当的if子句。 如果需要更多信息,请告诉我

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
var xmlHttp;

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

if(!xmlHttp)
    alert("cant create object");
else
    return xmlHttp;


}

function process_search()
{
if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
{
    search_parameter = encodeURIComponent(document.getElementById("userInput").value);
    search_type = encodeURIComponent(document.getElementById("userOptions").value);
    xmlHttp.open("GET", "../pages/search_xml.php?search_parameter=" + search_parameter +      "&search_type=" + search_type, true);
    xmlHttp.onreadystatechange = handleServerResponse;
    xmlHttp.send(null);
}
else
{
    setTimeout('process_search()',5000);
}
}

function handleServerResponse()
{
if(xmlHttp.readyState==4)
{
    if(xmlHttp.status==200)
    {
        xmlResponse = xmlHttp.responseXML;
        root = xmlResponse.documentElement;

        if(document.getElementsByTagName('find_users'))   // FIND USERS
        {
            first_name = root.getElementsByTagName('first');
            last_name = root.getElementsByTagName('last');

            users = document.createElement('ul');
            users.setAttribute("id", "usersFound");

            document.getElementById("underInput").innerHTML = "";   //RESETS THE DIV BEFORE INSERTING DATA
            for(var i=0; i< first_name.length; i++)
            {
                usersList = document.createElement('li');
                t = document.createTextNode(first_name.item(i).firstChild.data + " - " + last_name.item(i).firstChild.data + "<br/>");
                usersList.appendChild(t);
                underInput = document.getElementById("underInput");

                underInput.appendChild(usersList);
            }
        }else if(document.getElementsByTagName('find_config_item'))   //FIND CONFIG ITEMS
        {
            item = root.getElementsByTagName('item');
            desc = root.getElementsByTagName('description');

            itemsList = document.createElement('ul');
            itemsList.setAttribute("id", "itemsFound");

            document.getElementById("underInput").innerHTML = "";   //RESETS THE DIV BEFORE INSERTING DATA
            for(var i=0; i< item.length; i++)
            {
                itemList = document.createElement('li');     // CREATE LIST ITEM ELEMENT
                t = document.createTextNode(item.item(i).firstChild.data + " - " + desc.item(i).firstChild.data + "<br/>");
                itemList.appendChild(t);
                underInput = document.getElementById("underInput");

                underInput.appendChild(itemList);
            }
        }


        setTimeout('process_search()', 5000);
    }
    else
    {
        alert("something is wrong");
    }
}
}
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject()
{
var-xmlHttp;
if(window.ActiveXObject)
{
尝试
{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}捕获(e)
{
xmlHttp=false;
}
}
其他的
{
尝试
{
xmlHttp=新的XMLHttpRequest();
}捕获(e)
{
xmlHttp=false;
}
}
如果(!xmlHttp)
警报(“无法创建对象”);
其他的
返回xmlHttp;
}
函数进程_搜索()
{
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4)
{
search_参数=encodeURIComponent(document.getElementById(“userInput”).value);
search_type=encodeURIComponent(document.getElementById(“userOptions”).value);
xmlHttp.open(“GET”、“./pages/search_xml.php?search_parameter=“+search_parameter+””和search_type=“+search_type,true);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}
其他的
{
setTimeout('process_search()',5000);
}
}
函数handleServerResponse()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
xmlResponse=xmlHttp.responseXML;
root=xmlResponse.documentElement;
if(document.getElementsByTagName('find_users'))//查找用户
{
first_name=root.getElementsByTagName('first');
last_name=root.getElementsByTagName('last');
users=document.createElement('ul');
users.setAttribute(“id”、“usersFound”);
document.getElementById(“underInput”).innerHTML=“”;//在插入数据之前重置DIV
对于(变量i=0;i”;
usersList.appendChild(t);
underInput=document.getElementById(“underInput”);
underInput.appendChild(usersList);
}
}else if(document.getElementsByTagName('find\u config\u item')//查找配置项
{
item=root.getElementsByTagName('item');
desc=root.getElementsByTagName('description');
itemsList=document.createElement('ul');
itemsList.setAttribute(“id”、“itemsFound”);
document.getElementById(“underInput”).innerHTML=“”;//在插入数据之前重置DIV
对于(变量i=0;i);
项目列表。追加子项(t);
underInput=document.getElementById(“underInput”);
输入不足。追加子项(项目列表);
}
}
setTimeout('process_search()',5000);
}
其他的
{
警惕(“有什么不对劲”);
}
}
}

对于这个用例,您不应该真正依赖try/catch。您可以相当肯定XMLHttpRequest或Mircrosoft.XMLHTTP对象将存在,因此可以将代码简化为以下内容:

function createXmlHttpRequestObject () {
    return window.XMLHttpRequest ? new XMLHttpRequest() : new XDomainRequest();
}

var xmlHttp = createXmlHttpRequestObject();

让我知道你是否想看一个非三元版本

谢谢..工作完美,我理解它…关于我的if-else-if问题的原始问题如何。如果我运行它,它仍然有相同的问题]我不知道你在问什么对不起。你能澄清一下吗?