Jump to content
Larry Ullman's Book Forums

Ajax's Onreadystatechange Return Uncaught Typeerror While Trying The Example In The Book


Recommended Posts

getXMLHttpRequestObject returns nothing, which means it returns undefined. The ajax variable inside that function is not the same as the one outside. So I have to return variable ajax at the end of both functions.

 

window.onload = function(){

     'use strict';
     var ajax = getXMLHttpRequestObject();
     ajax.onreadystatechange = function(){
         if(ajax.readyState == 4){
             if((ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304)){
 

                 document.getElementById('output').innerHTML = ajax.responseText;
             }else{
                 document.getElementById('output').innerHTML = 'Error ' + ajax.statusText;
             }
         }
     };
         document.getElementById('btn').onclick = function(){
         ajax.open('GET', 'resources/test.txt', true);
         ajax.send(null);
     };

     //return var ajax
     **return ajax;**
 }
 function getXMLHttpRequestObject(){
     var ajax = null;
     if(window.XMLHttpRequest){
         ajax = new XMLHttpRequest();
     }else if(window.ActiveXObject){
         ajax = new ActiveXObject('MSXML2.XMLHTTP.3.0');
     }

     //return var ajax
     **return ajax**
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...