SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX response not hitting ready state 4

    I'm diving into the world of asynchronous JS communication, and everything appears to be working except my response state never hits 4.

    I encode my data into a string called "post" which I send to the server like such:
    Code JavaScript:
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", post.length);
    req.setRequestHeader("Connection", "close");
    req.send(post);
    with req of course being my XMLHttpRequest object. This data is sent to a PHP script which processes it, and then simply writes back (echoes to the page) "SUCCESS." I know that the query goes through successfully because it updates the database with the correct values from the query string.

    For debugging I made my object alert its state onchange:
    Code JavaScript:
    req.onreadystatechange = function() {
    	if( req.readyState == 4 ) {
    		if( req.status == 200 || req.status == 304) {
    			//update user interface
    		} else {
    			alert('The server encountered an error processing your request');
    		}
    	} else {
    		alert( req.readyState );
    	}
    };
    The script alerts 1, then 2, then 3, and then it stops. The query goes through, SUCCESS comes back from the request, but it never hits 4! Is this a race condition? I'm running this on localhost, but if I add sleep(10) to my PHP code before the echo then the execution simply changes to:
    alert 1, (10 second pause), alert 2, alert 3, stop. If I remove my alerts it does the same thing, just stops, but the query goes through as is evident on page refresh.

    Here are my firebug headers, not sure if that helps anything...shows SUCCESS coming back and the headers (dunno if they indicate a problem)
    http://www.delvarworld.com/stuff/ajax.JPG

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to call req.open before the other functions. Since you're sending data, you need to set the http-method to POST.
    Code:
    req.open("POST", url, true);

  3. #3
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do, that was just an excerpt. Here's a larger view:

    Code JavaScript:
    // (code to build what page to go to and correct item id)
    req.open('POST', page+id+'/edit', true);
    req.onreadystatechange = function() {
    	if( req.readyState == 4 ) {
    		if( req.status == 200 || req.status == 304) {
    			//update user interface
    		} else {
    			alert('The server encountered an error processing your request');
    		}
    	} else {
    		alert( req.readyState );
    	}
    };
    var post='';
    // (for loop to take data from the page and put it into query string) ?var=value format
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", post.length);
    req.setRequestHeader("Connection", "close");
    req.send(post);

  4. #4
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DelvarWorld View Post
    Code JavaScript:
    req.setRequestHeader("Connection", "close");
    Could this be the culprit??
    MrBaseball34
    Hook'Em Horns!

  5. #5
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, actually the culprit would be me, being an idiot. While commenting out that line I realized that it doesn't do anything when it hits 4 because there's no actual code there. It's really just that comment. I forgot it alerted the state in an else, so when it hits 4 and is successful nothing happens.

    *slaps self*

  6. #6
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sometimes, the solution is right before your own eyes. It has happened to me numerous times.
    MrBaseball34
    Hook'Em Horns!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •