新的XMLHttpRequest无法在Firefox上运行

时间:2010-10-15 08:44:51

标签: javascript html xml ajax

下面的脚本在IE中完美地用于XML响应。但似乎我无法弄清楚如何在Firefox或Chorme上运行。尝试一些修改,但仍然无法运行它。请需要助手。

<script type="text/javascript" language="javascript"> 
var xmlhttp;
var timeStamp;

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate() //remove the + 1  afterwards
var year = currentTime.getFullYear()
var hour = currentTime.getHours()
var minutes = currentTime.getMinutes()
var second = currentTime.getSeconds() + 1


timeStamp = day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + second;



function on_click()

{



    var xmlToSend = "<?xml version='1.0' encoding='utf-8'?>";
    xmlToSend += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' ";
    xmlToSend += "xmlns:xsd='http://www.w3.org/2001/XMLSchema' ";
    xmlToSend += "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
    xmlToSend += "<soap:Body><Welcomescreen Sender='SENDERDDRESS' TimeStamp='28/10/2009 16:49:31' Type='1' Workshop='SG' RequireAppointmentDate='2010/01/04' xmlns='http://www.SENDERDDRESS.com/integration'/>";
    xmlToSend += "</soap:Body></soap:Envelope>";

    /


    var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmldoc.loadXML(xmlToSend); 


    if (window.XMLHttpRequest)
      {/ / code
    for IE7 + , Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}


xmlhttp.onreadystatechange = state_Change;

xmlhttp.open("POST", "http://SENDERDDRESS:4509/resd", false);
xmlhttp.setRequestHeader("SOAPAction", "http://www.mhe.com/SRP/requestVinRequest");
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.setRequestHeader("User-Agent", "Jakarta Commons-HttpClient/3.0.1");
xmlhttp.setRequestHeader("Host", "SENDERDDRESS:4509");
xmlhttp.setRequestHeader("Content-Length", "391");
xmlhttp.send(xmldoc);



var objResponse = document.getElementsByTagName("Appointment");

objResponse.innerText = xmlhttp.responseXML.xml;

}

function state_Change()

{
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            txt = "<table align='right' border='1' width='400'><tr><th><font color='#d9d7d7' size='4' face='verdana'>Time</font></th><th><font color='#d9d7d7' size='4' face='verdana'>Plate No.</font></th><th><font color='#d9d7d7' size='4' face='verdana'>Status</font></th></tr>";
            x = xmlhttp.responseXML.documentElement.getElementsByTagName("Appointment");
            for (i = 0; i < x.length; i++) {

                xx = x[i].getElementsByTagName("AppointmentTime"); {
                    try {
                        txt = txt + "<td><font size = 5>" + xx[0].firstChild.nodeValue + "</font></td>";
                    } catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }

                xx = x[i].getElementsByTagName("NumberPlate"); {
                    try {
                        txt = txt + "<td><font size = 5>" + xx[0].firstChild.nodeValue + "</font></td>";
                    } catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }

                xx = x[i].getElementsByTagName("statusCode"); {
                    try {
                        txt = txt + "<td><font size = 5>" + xx[0].firstChild.nodeValue + "</font></td>";
                    } catch (er) {
                        txt = txt + "<td> </td>";
                    }
                }
                txt = txt + "</tr>";
            }
            txt = txt + "</table>";
            document.getElementById('txtCDInfo').innerHTML = txt;
        } else

        {


        }

    }

}
</script> 

1 个答案:

答案 0 :(得分:2)

我看到两个问题。一个是你在一系列/行之后有一个迷路xmlToSend +=,这是一个语法错误,然后就是这样:

var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 

ActiveXObject不是标准的,它是IE特定的东西。与您使用它的其他地方不同,该行不是有条件的。

您只需将xmlToSend字符串直接传递到XMLHttpRequest#sendlink),您就不需要首先制作XML文档。它只需要再次转回一个字符串即可发送。

如果您确实想要实际创建XML文档对象,则可以在兼容的浏览器上使用DOMImplementation#createDocument(例如document.implementation.createDocument)。

偏离主题:JavaScript库可以让您在Ajax领域(以及许多其他领域)的生活更轻松。像jQueryClosurePrototypeYUIany of several others之类的内容可能会为您节省一些时间。