Jump to content
Larry Ullman's Book Forums

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

Recommended Posts

Uncaught TypeError: Cannot set property 'onreadystatechange' of undefined

    at window.onload (test.js:4). 


The above error message is what is displayed on the console while trying to run the ajax example on page 433 of the book. What can be done to rectify it? Any clue or answer will be appreciated.

Share this post

Link to post
Share on other sites

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;
                 document.getElementById('output').innerHTML = 'Error ' + ajax.statusText;
         document.getElementById('btn').onclick = function(){
         ajax.open('GET', 'resources/test.txt', true);

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

     //return var ajax
     **return ajax**

Share this post

Link to post
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.

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.


  • Create New...