Jump to content
Larry Ullman's Book Forums

Chapter 11 - Script Stalls At Ajax.Send(Data)


Recommended Posts

Have a situation where - when submitting form data to a php script - the ajax script stalls at:

ajax.send(data);

When the script stalls, it throws no error messages and returns no results. It just stalls. The php script runs all of its scripty things perfectly sans ajax, so I know the php and mysql is solid. It's just when it comes to incorporating the ajax that there is any issue.

 

But wait - there's more.

 

When the script is under the influence of ajax, and stalls, the Firebug console displays this line of text in red (red obviously indicating some kind of error, even though there's no error message given):

 

"POST mysite/foo.php               script.js (line 60)"

 

The reference to (line 60) is the very line where the aforementioned ajax.send call resides.

 

Now, here's where things get really screwy:

 

If I go into Firebug, right-click on that red console line (shown above) and select 'resend' - everything works perfectly.

 

What the . . .heck?

 

Is there something obvious happening here that everyone (besides me) knows, or should I post some of the code for further scrutiny?

 

 ~ David

Link to comment
Share on other sites

Could you please post the surrounding code, including how a value is assigned to the data variable?

 

 

When you say "post . . . how a value is assigned to the data variable", do you mean this?

function sendThingy() {
	
	'use strict';
    
    // Get form elements:
    var comments = document.getElementById('comments');
    var user = document.getElementById('user');
	
	if (comments.value.length > 0) {
	
		var ajax = getXMLHttpRequestObject();

		ajax.onreadystatechange = function() {

			if (ajax.readyState == 4) {
	
				if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) {

					document.getElementById('chat').innerHTML = ajax.responseText;

				} else {
		
					document.getElementById('chat').innerHTML = 'Error: ' + ajax.statusText;
				}
			}
		};
		
		ajax.open('POST', 'http://www.example.com/foo.php', true);
		
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var data = 'comments=' + encodeURIComponent(comments.value) + '&user=' + encodeURIComponent(user.value);
		
		ajax.send(data);
		
		return false;
	
	} else {
	
		alert('You forgot to enter a comment');
		
		return false;
	}
}

 ~ David

Link to comment
Share on other sites

If you alert the data variable to the screen right before the ajax.send method call, is the data string what you expect?

Try reducing your foo.php script down to literally a single echo statement like the following:

<?php
  
  echo 'Just testing if this string is returned.';

And then trying alerting the Ajax response text right after the "if ( (ajax.status >= 200 ..." line, and see if you get the expected response back. If you do, then you can go from there with your debugging.

Link to comment
Share on other sites

Suggestion (1): alert the data variable to the screen right before the ajax.send method call . . .

Suggestion (2): Try reducing your foo.php script down to literally a single echo statement like the following:

echo 'Just testing if this string is returned.';

Suggestion (3): alert the Ajax response text right after the "if ( (ajax.status >= 200 ..." line . . .

 

 

The result of implementing Suggestion (1) is:

 

mysite/foo.php?comments=test%20string&user=1

 

The string looks to be constructed as one should expect.

 

=====================================================

 

The result of implementing Suggestion (2) is:

 

"Just testing if this string is returned."

 

The php page looks to return the expected value.

 

=====================================================

 

Alerting the ready.State variable produces the following sequence:

 

ready.State is: 1

ready.State is: 2

ready.State is: 4

 

=====================================================

 

And *here* is the interesting part:

 

"ajax.status is: 0"

 

For some reason ajax.status does not move beyond goose-egg.

 

Back to the drawing board (or in this case, keyboard).

 

 

 

 ~ David

Link to comment
Share on other sites

 Share

×
×
  • Create New...