0

我正在尝试编写类似 javaScript 插件的谷歌广告。我不能使用 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

4 回答 4

1

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

于 2013-04-06T19:25:17.060 回答
0

在对响应进行任何操作之前,最好确保状态码为 200。然后你可以更早地发现问题。

于 2013-04-06T19:27:30.253 回答
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);
于 2013-04-06T19:40:30.563 回答
0

通过检查我的 html 代码解决了与此类似的问题。我的表单中有一个onclick处理程序,将按钮提交给一个方法。像这样:onclick="sendFalconRequestWithHeaders()"。这个方法反过来调用 ajax 就像你的一样,做我想做的事。但与预期不同,我的浏览器没有返回任何内容。

某人的辛勤工作中学到,我在这个处理程序中返回了错误,并解决了。让我提一下,在到达这篇文章之前,我花了整整 3 天的周末和半天的时间在办公室编写代码实现CORS filters、、jetty config其他jersey and embedded jetty相关的东西——只是为了解决这个问题。我所有的理解都围绕着cross domain ajax requests标准的东西。javascript 中的简单错误让你变得愚蠢,这太荒谬了。

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

于 2013-11-25T08:49:29.750 回答