bozebo Posted October 21, 2007 Share Posted October 21, 2007 I have never been able to get ajax to work, I have followed about 50 tutorials and nothing ever happens, I need some help. I have vastly simplified what I am trying to do into a test, Ill I want it to do is alert if the page it reads has "1" in it... Shouldn't that be simple? My most recent attempt is modeled closely on the tutorial on ajaxfreaks.com ("Simple Introduction to AJAX and XMLHttpRequest") function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('Problem creating the XMLHttpRequest object'); } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); function sendRequest(act) { // Open PHP script for requests http.open('get', 'other/is_in_game.php?gameid='+act); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.status == 200){ if(http.readyState == 4){ // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content alert('IT WORKED!!!'); } } } else { alert('Something went wrong'); } } The page it reads (http://www.bozebo.com/knightlands/other/is_in_game.php) contains simple php to set the headers correctly, and it prints "1" on the page.. so the whole file through http is jus "1".. So my ajax script should work every time without fail?? It doesn't. You can find my test here: http://www.bozebo.com/knightlands/ajaxtest.html Can anyone help me? Or am I just cursed... Quote Link to comment Share on other sites More sharing options...
markjoe Posted October 23, 2007 Share Posted October 23, 2007 I have recently had troubles with getting AJAX going for the first time. I dropped the check for http.status == 200. I only wait for http.readystate to reach 4. Then it worked for me. In the code you posted, I see where you are creating the XMLHttpRequest object, but I don't see you ever send the request. The function to send the request is there, but it doesn't get called. What are function are you calling from the page? Quote Link to comment 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.