问题
I am working on a Web App in which I need to call to the server cgi which will in turn use this data for further use. It working fine in IE6+ and FF but not in chrome 15.(not yet checked with IE6) Here is my code :
var destinationURL = "/cgi-bin/conf.cgi";
var xmlHttpReq = getXMLHttpRequest();
if(xmlHttpReq == null){
return false;
}
if (xmlHttpReq.readyState == 0){
xmlHttpReq.open('POST', destinationURL, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState == 4) {
alert(xmlHttpReq.responseText);
}
}
xmlHttpReq.send();
}
And getXMLHttpRequest() function is :
function getXMLHttpRequest() {
var xmlHttpReq;
if (window.XMLHttpRequest) {
xmlHttpReq = new window.XMLHttpRequest();
}
else {
try {
xmlHttpReq = new window.ActiveXObject("Microsoft.XMLHTTP");
}
catch(ex) {
alert('Exception in initializing the XMLHttpRequest object '+ex.toString());
return null;
}
}
return xmlHttpReq;
}
Any solutions ? Thanks in advance...
回答1:
I will use the following code in getXMLHttpRequest() method:
var xmlhttp;
if(navigator.appName == "Microsoft Internet Explorer") {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
} else {
xmlhttp = new XMLHttpRequest();
}
Try that and let us know
来源:https://stackoverflow.com/questions/8033263/http-post-xhr-not-working-in-chrome