HTTP-POST XHR not working in chrome

非 Y 不嫁゛ 提交于 2019-12-11 16:21:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!