HTTP-POST XHR无法在chrome中运行

时间:2011-11-07 06:11:33

标签: google-chrome cross-browser xmlhttprequest

我正在开发一个Web应用程序,我需要调用服务器cgi,然后使用这些数据进一步使用。 它在IE6 +和FF中工作正常,但在chrome 15中没有。(尚未用IE6检查) 这是我的代码:

 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();                
    } 

getXMLHttpRequest()函数是:

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

任何解决方案? 提前谢谢......

1 个答案:

答案 0 :(得分:0)

我将在getXMLHttpRequest()方法中使用以下代码:

var xmlhttp;
if(navigator.appName == "Microsoft Internet Explorer") {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
} else {
    xmlhttp = new XMLHttpRequest();
}

试试并告诉我们

相关问题