Arif
Arif

Reputation: 11

new XMLHttpRequest cannot run on Firefox

The script below work perfectly in IE for the XML respond. But seems i cant figured out how to run on Firefox or Chorme. Try few modification but still not able to run it. Kindly need assistant.

<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> 

Upvotes: 1

Views: 1430

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

I see two issues. One is that you have a stray / just after the series of xmlToSend += lines, which is a syntax error, and then there's this:

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

ActiveXObject is not standard, it's an IE-specific thing. Unlike the other place you're using it, that line is not conditional.

You can just pass the xmlToSend string directly into XMLHttpRequest#send (link), you don't need to make an XML document out of it first. It'll just have to get turned back into a string again to be sent.

If you really want to actually create an XML document object, you can use DOMImplementation#createDocument (e.g., document.implementation.createDocument) on compliant browsers.

Off-topic: JavaScript libraries can make your life a bit easier in the Ajax area (and many others). Something like jQuery, Closure, Prototype, YUI, or any of several others may save you some time.

Upvotes: 2

Related Questions