开发者

No response from XMLHttpRequest

I'm working on a Firefox extension and I'm stuck at trying to get the response text from a few sites that I need to get data from. Not sure why I'm not getting any data here.

Here's the code to test out an XHR request from Quantcast.com:

function callback(serverData) {       
   alert(serverData);                 
}

function ajaxRequest() {
   var AJAX = null;                           
   if (window.XMLHttpRequest) {     开发者_开发百科          
      AJAX=new XMLHttpRequest();              
   } else {                                   
      AJAX=new ActiveXObject("Microsoft.XMLHTTP"); 
   }                                               
   if (AJAX==null) {                               
      alert("Your browser doesn't support AJAX."); 
      return false                                 
   }
   AJAX.onreadystatechange = function() {                  
      if (AJAX.readyState==4 || AJAX.status==200) { 
         callback(AJAX.responseText);            
      }                                          
   }
   var url='http://www.quantcast.com/facebook.com';
   AJAX.open("GET", url, true);                    
   AJAX.send(null);                                
}

Is there something missing here? I know of other extensions that are pulling data through Quantcast via XHR, but when I'm trying to do this nothing is showing up on the alert.


First I would do the opposite when making your call.

try {
   AJAX = new ActiveXObject("Microsoft.XMLHTTP"); // Try Internet Explorer 
}
catch(e) // Failure then it is something else.
{
  AJAX = new XMLHttpRequest();
}

Then I would also change the way you get the answer because you want readyState and status, not or as you put it.

AJAX.onreadystatechange  = function()
{ 
    var xhrdata = "";
     if(AJAX.readyState  == 4)
     {
          if(AJAX.status  == 200) 
             xhrdata = AJAX.responseText; 
          else 
             xhrdata = AJAX.status;
     }
};

you could event put a try too and catch(e) for sending a message on Server Error. you can replace the xhrdata with the callback function too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜