gbengasupowale807 0 Posted April 27, 2018 Report Share Posted April 27, 2018 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. Quote Link to post Share on other sites
gbengasupowale807 0 Posted April 27, 2018 Author Report Share Posted April 27, 2018 have been able to solve the problem Quote Link to post Share on other sites
Larry 428 Posted April 27, 2018 Report Share Posted April 27, 2018 Excellent! Could you kindly share the solution so that others might benefit? Thanks! 1 Quote Link to post Share on other sites
gbengasupowale807 0 Posted April 30, 2018 Author Report Share Posted April 30, 2018 Excellent! Could you kindly share the solution so that others might benefit? Thanks! I will do just that sir Quote Link to post Share on other sites
gbengasupowale807 0 Posted April 30, 2018 Author Report Share Posted April 30, 2018 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**} Quote Link to post Share on other sites
Larry 428 Posted April 30, 2018 Report Share Posted April 30, 2018 Ah, excellent. Thanks so much for sharing! Quote Link to post Share on other sites
Recommended Posts
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.