【问题标题】:ResponseXML returned as null or undefinedResponseXML 返回为 null 或未定义
【发布时间】:2011-10-21 09:16:59
【问题描述】:

我正在开发一个实时修补程序,但我卡住了。

应用程序架构的快速解释:脚本向 dispatcher.php 发送一个 XML 请求。在调度程序的顶部,内容类型设置为 text/xml。然后根据请求中的属性设置一条命令:

<request type="buworkflows"><unit>10</unit></request>

因此对于 buworkflows 命令,调用 db 并返回包含数据的数据集。

应该使用此返回的数据动态生成一个下拉列表。但是,我在 IE 中得到“需要对象”。以下代码在我的开发服务器上的 IE 和 FF 中运行良好,但在现场运行时失败。我无法在现场使用 FF 进行调试;应用程序是为 IE 量身定制的,任何 mods 都意味着我会影响实时系统。所以,我现在必须使用 IE。我已经提醒了单位 ID 和 XML 请求;一切皆好。此特定请求仅对一种类型的数据集失败;其余工作正常。

我希望这些信息足以提供评论和反馈。

真的需要你们的帮助。

***最终更新:我昨天解决了这个问题。请参阅我对詹姆斯的评论中的解释。感谢您的时间和帮助。已解决。

更新:WS_DISPATCHER 解析为 /ws/dispatcher.php。我已经通过调试确认它正在正确地传递到 AJAX。

更新:为了完成,getRequestObject()

 function getRequestObject() {
resetMsgBar();
var req = null;
if(XMLHttpRequest) {
    req = new XMLHttpRequest();
    if(req.overrideMimeType)
        req.overrideMimeType("text/xml");
}
else if(window.ActiveXObject) {
    try {
        req = new ActiveXObject("msxml2.xmlhttp");
    } catch(e) {
        try {
            req = new ActiveXObject("microsoft.xmlhttp");
        } catch(e) {}
    }
}
if(!req)
    return null;
else
    return req;

}

AJAX 代码:

                            var req = getRequestObject();
                            var reqXml = new String();
                            reqXml+= "<request type='buworkflows'>";
                            reqXml+= "<unit>" + id + "</unit>";
                            reqXml+= "</request>";
                            req.open("POST", "<?=WS_DISPATCHER_ADDR?>", true);
                            req.onreadystatechange = function() {
                                if(req.readyState==4) {
                                    if(req.status==200) {
                                        //var wfData = new ActiveXObject("microsoft.xmldom");
                                        //wfData.async = false;
                                        //wfData.load(req.responseXml);
                                        var opt = null;
                                        document.getElementById("type").options.length=0;
                                        opt = document.createElement("option");
                                        opt.text ="Please select the assignment type";
                                        opt.value="-1";
                                        document.getElementById("type").options.add(opt);
                                        //if(wfData.documentElement) {    
                                            for(var i=0; i < req.responseXML.documentElement.childNodes.length; i++) {
                                                opt = document.createElement("option");
                                                var elem = req.responseXML.documentElement.childNodes[i];
                                                opt.value = elem.childNodes[0].childNodes[0].nodeValue;
                                                opt.text = elem.childNodes[2].childNodes[0].nodeValue;
                                                if (undefined != elem.childNodes[3].childNodes[0]){
                        opt.title = elem.childNodes[3].childNodes[0].nodeValue;
                      }
                      document.getElementById("type").options.add(opt);                                              
                      //}                                                                           
                                                //opt.text = req.responseXML.documentElement.childNodes[i].firstChild.selectSingleNode("name").text;                                                    
                                                //opt.value=req.responseXML.documentElement.childNodes[i].selectSingleNode("id").text;
                                                //opt.title=req.responseXML.documentElement.childNodes[i].selectSingleNode("description").text;
                                                //document.getElementById("type").options.add(opt);
                                            }
                                        //}
                  loadAdditionalFields(0);
                                        hideProgressBar();
                                    }
                                }
                            }
            req.send(reqXml);

【问题讨论】:

  • 为 getRequestObject() 添加实际代码。
  • 我建议您安装 Fiddler2 并检查您在实时环境中从 ajax 调用获得的实际响应,包括标题和内容。这可能会给你一个线索。
  • responseText 包含什么?
  • @RoToRA:包含实际的 XML。请参阅原始帖子中对詹姆斯的回答和我的笔记的评论。

标签: php javascript xml ajax


【解决方案1】:

通过添加调试:

if(req.status==200) {
  alert(req.responseText);
  alert(req.responseXML);

并查找 php 错误

【讨论】:

  • responseText 包含实际的 XML。插入时没有捕获或删除一个 duff 非 ASCII 字符,因此它在调度程序生成 XML 数据集时使其无效。现在解决了。
【解决方案2】:

在通过代码进行更多调试并检查数据库中的数据后,我昨天解决了这个问题。有一个非 ASCII 字符使 XML 数据集无效。最初的程序员没有将节点值包含在 CDATA 块中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2011-06-03
    相关资源
    最近更新 更多