2011-11-07 50 views
0

我正在开发一个Web应用程序,在这个应用程序中我需要调用服务器cgi,然后将这些数据用于进一步的使用。 它在IE6 +和FF,但无法在Chrome 15(与IE6尚未检查) 这里工作的罚款是我的代码:HTTP-POST XHR不能在chrome中工作

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

任何解决方案? 在此先感谢...

+0

我从POST将其改为GET和它的工作... !!! 但是如果我想严格使用POST? – Ved

回答

0

我将使用下面的代码在getXMLHttpRequest()方法:

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

尝试这一点,让我们知道

+0

我已经试过你的代码,但仍然没有得到预期的结果。 – Ved