开发者

Multiple xmlhttprequest on a page

I am having an error when using new XMLHttpRequest() for the second time in JavaScript code called from textbox event on page. My JavaScript finds suggestions for text entry from the SQL to do that I use xmlhttprequest, it does fine when it is the first time but when I keep typing in the text box I receive:

"typeerror: xmlhttprequest not a costructor"

(this error happens only in Firefox)

This is my code:

function fnNull() { }; 
function changeofstate(){
   if (XMLHttpRequest.readyState == 4) 
           {
whatever ;

}
 XMLHttpRequest.onreadystatechange = fnNull();
}
function whentextchange(){
   var WebURL = "the url here ";
         XMLHttpRequest = CreateXmlHttpObject(changeOfState);
         XMLHttpRequest.open("GET", WebURL, true);
         XMLHttpRequest.send(null);
         XMLHttpRequestt.abort();
   }


}

function CreateXmlHttpObject(handler) {    
       var objXmlHttpReq = null;
       var Req = null;

                   if (navigator.userAgent.indexOf("Opera")>=0)
                   {                  
                   return ;
                  }
                  if (navigator.userAgent.indexOf("MSIE")>=0)
                  { 
                   var strName="Msxml2.XMLHTTP";
                   if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
                   {
                   strName="Microsoft.XMLHTTP";
                   } 
                  try
                   {
                     
                   objXmlHttpReq=new ActiveXObject(strName);
                   objXmlHttpReq.onreadystatechange = handler;
                 
                   return objXmlHttpReq;
                  } 
                  catch(e)
                  {                
                   return ;
                   } 
   开发者_如何转开发                }
                   if (navigator.userAgent.indexOf("Mozilla") >= 0) {
                     try
                   {
                            if (Req == null) {
                          Req = new XMLHttpRequest();
                       }
                       
                             Req.onload = handler;
                           Req.onerror = handler;
                           return Req;
                
                       }
                       catch (e) {
                           alert(e);
                       alert(Req.responseText)
                           alert(e);
                           return;
                       } 
                   }
   } 


You should name your request object something else than XMLHttpRequest. It might override the XMLHttpRequest object in the browser. Thus giving you the error.

XMLHttpRequest = CreateXmlHttpObject(changeOfState);

Assigning XMLHttpRequest variable like this is actually using global scope. You should use var and another variable name

var req = CreateXmlHttpObject(changeOfState);

Hope this clarifies.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜