XMLHttpRequest状态4但responseText为空

时间:2013-04-06 19:13:46

标签: php javascript xmlhttprequest

我正在尝试编写像javaScript插件这样的Google广告。我不能使用jQuery ajax,因为它应该适用于所有网站。 这是我的JavaScript代码。

var ajaxRequest;  // The variable that makes Ajax possible!

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        alert( ajaxRequest.responseText);
        console.log('xhr',ajaxRequest)
    } else {
        //alert(ajaxRequest.readyState);
        //alert(ajaxRequest.responseText);
    }
}
    var project_path = "http://www.domainname.com/"; //for stackoverflow, using right path in live code.
var req_url = project_path + "ads/verifypublisher/";
ajaxRequest.open("GET", req_url, true);
ajaxRequest.send(null); 

以下是我的PHP文件的样子,

echo("<b>hi:</b> ");
exit();

是的,那就是我所拥有的一切。仍然responseText是空的。我在这里缺少什么?

4 个答案:

答案 0 :(得分:1)

alert(req_url)应显示错误的网址“http://www.domainname.comads/verifypublisher/

答案 1 :(得分:0)

在对响应做任何事情之前,最好确保状态代码为200。然后你可以提前发现问题。

答案 2 :(得分:0)

在我的测试中尝试这个,它可以在大多数浏览器中使用

var responseText=function(e,doing){
                     if (e.readyState== 4) {
                           if(doing)doing(e);
                           return(e.responseText);              
                     };
                     if (e.readyState == 200) { 
                           if(doing)doing(e);               
                           return(e.responseText);

                     };
                     return false;                   
};
function  loadXMLPostDocf(url,method,posData,uploadProgress,uploadComplete,uploadFailed,uploadCanceled,statechange,CallBackOnSend) {
   var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest && checkVersion()) {
    pos = new XMLHttpRequest(); 
    pos.open(method, url, true);
    //pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    if(statechange!=''){pos.onreadystatechange = statechange;};
    try{
      if(uploadProgress!='')if(pos.upload){
          pos.upload.addEventListener("progress", uploadProgress, true);
      }else{
          pos.addEventListener("progress", uploadProgress, true);
      }
      if(uploadComplete!='')pos.addEventListener("load", uploadComplete, false);
      if(uploadFailed!='')pos.addEventListener("error", uploadFailed, false);
      if(uploadCanceled!='')pos.addEventListener("abort", uploadCanceled, false);
    }catch(e){

    }

    pos.send(posData);
    if(CallBackOnSend)CallBackOnSend(pos);
    //console.log(pos.abort());
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {  
         for (var i=0; i < arrSignatures.length; i++) {
                    try {
                        pos = new ActiveXObject(arrSignatures[i]);

                    } catch (oError) {
                        //ignore
                    };
          };
    if (pos) {
        pos.open(method, url, false);
        //pos.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        if(!statechange){pos.onreadystatechange = statechange;};
        if(!uploadComplete){pos.onreadystatechange = uploadComplete;};
       // if(uploadProgress!='')pos.upload.addEventListener("progress", uploadProgress, true);
        //if(uploadComplete!='')pos.addEventListener("load", uploadComplete, false);
        //if(uploadFailed!='')pos.addEventListener("error", uploadFailed, false);
        //if(uploadCanceled!='')pos.addEventListener("abort", uploadCanceled, false);
        pos.send(posData);
        if(CallBackOnSend)CallBackOnSend(pos);
    };   
 };
 return(pos);
};

样品:

 var complete=function(e){
    var Body=responseText(e.target);
    console.log(Body);
};
var uploadProgress=function(evt){

};
var uploadComplete=function(evt){
    if(evt.target.status==200 || evt.target.status==304){
      var Body=responseText(evt.target);
      console.log(Body);
    }else{
        return false;
    };
};
var uploadFailed=function(evt){

};
var uploadCanceled=function(evt){

};
var CallBackOnSend=function(evt){

};
loadXMLPostDocf("http://domain.com/index.php","GET",null,uploadProgress,uploadComplete,uploadFailed,uploadCanceled,complete,CallBackOnSend);

答案 3 :(得分:0)

通过检查我的HTML代码解决了与此类似的问题。我在表单的提交按钮中有一个onclick处理程序。像这样:onclick="sendFalconRequestWithHeaders()"。这个方法反过来调用ajax就像你的一样,做我想要的。但不像预期的那样,我的浏览器什么也没回来。

someone's hardwork了解到,我在这个处理程序中返回了false,并解决了。 让我提一下,在到达这篇文章之前,我已经花了整整3天的周末和半天在办公室编写实现CORS filtersjetty config,其他jersey and embedded jetty相关内容的代码 - 只是解决这个问题。围绕cross domain ajax requests和标准内容循环我的所有理解。简单的javascript错误让你愚蠢是荒谬的。

现在我必须清理我的代码以干净地提交git补丁。谢谢那个人。