fugix Posted May 18, 2011 Share Posted May 18, 2011 Hey guys, im starting to learn ajax and im trying an example that is not working for me..here is my script.. <span id='ajaxButton' style='cursor:pointer; text-decoration: underline'>Make a Request</span> <script type='text/javascript'> (function() { var httpRequest; document.getElementById("ajaxButton").onclick = function() { makeRequest('test.html'); }; function makeRequest(url) { if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { httpRequest = new ActiveXObject("Msxml2,XMLHTTP"); } catch (e) { try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!httpRequest) { alert('Giving up cannot create an XMLHTTP instance'); return false; } httpRequest.onreadystatechange = alertContents; httpRequest.open('GET', url); httpRequest.send(); } function alertContents() { if (httpRequest.readyState === 4) { if(httpRequest.status === 20) { alert(httpRequest.responseText); } else { alert('There was a problem with the request'); } } } } )(); </script> I keep getting the alert "alert('There was a problem with the request');" as my result as opposed to the response text....if anyone can spot my error i would appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/236711-simple-ajax-script/ Share on other sites More sharing options...
fugix Posted May 18, 2011 Author Share Posted May 18, 2011 found the error..the status code needs to be 200 not 20...silly error Quote Link to comment https://forums.phpfreaks.com/topic/236711-simple-ajax-script/#findComment-1216817 Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2011 Share Posted May 18, 2011 In case you missed seeing it, there is a 'topic solved' button at the lower left-hand of the page. I got it for you this time. Quote Link to comment https://forums.phpfreaks.com/topic/236711-simple-ajax-script/#findComment-1216927 Share on other sites More sharing options...
fugix Posted May 18, 2011 Author Share Posted May 18, 2011 In case you missed seeing it, there is a 'topic solved' button at the lower left-hand of the page. I got it for you this time. thanks. I normally forget that its there Quote Link to comment https://forums.phpfreaks.com/topic/236711-simple-ajax-script/#findComment-1217164 Share on other sites More sharing options...
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.