Helpful Information
 
 
Category: Ajax and Design
AJAX Timers and Request Status Errors?

I am having all kinds of problems. :) Sorry guys. I am trying to build an AJAX automatic word search javascript. I have basically copied code from a book and tried it out. But I get and error. I will post my code and then explain more.



oHTTPPronounceReq = null;
iMyTimer = null;
category = 'Terminology';

function searchPronunciation(text){
if(iMyTimer != null){
clearTimeout(iMyTimer);
iMyTimer = null;
}

if(!oHTTPPronounceReq){
oHTTPPronounceReq = getXMLHTTPRequest();
}else if(oHTTPPronounceReq.readyState != 0){
oHTTPPronounceReq.abort();
}


var postStr = 't=' + text + '&category=' + category;


oHTTPPronounceReq.open("POST", "Pronunciation.php", true);

oHTTPPronounceReq.onreadystatechange = function (){
if(oHTTPPronounceReq.readyState == 4){
if(oHTTPPronounceReq.status == 200){
var response = oHTTPPronounceReq.responseText;
}
}
}

oHTTPPronounceReq.send(postStr);

iMyTimer = setTimeout(function () {
oHTTPPronounceReq.send(postStr);
}, 500);
}

function getXMLHTTPRequest() {
var requester = false;
try {
requester = new XMLHttpRequest();
}
catch (error) {
var aVersions = [ "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHTTP"];
for(var i = 0; i< aVersions.length;i++){
try {
requester = new ActiveXObject(aVersions[i]);
}
catch (error) {
continue;
}
}
}
return requester;
}


This is the error that I get


Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://flash.dev.mike/pronunciation/js/test.js :: anonymous :: line 25" data: no]
Source File: http://flash.dev.mike/pronunciation/js/test.js
Line: 25

I have the searchPronunciation firing on the onKeyUp event. I get this when I have typed too fast. If I type slow I don't have this problem.


Also for some strange reason my post str is not being received by my php file. The $_POST variable is empty. :(

Any ideas on what I have done wrong?

Thanks.

Michael

I explain the error message here:

http://radio.javaranch.com/pascarello/2006/02/07/1139345471027.html

If you run it in IE, you probably will get a status code.

Eric

Thanks,

I will check it out. I also noticed that I forgot to do

oHTTPPronounceReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

so I put it in and now I don't get any errors.

Thanks

Michael

Can I ask what is the server enviornment you are running this on?

Thanks,
Eric

I lied. I tried it again and when I type too fast it throws that error. I tried it in IE and got no error.

I did a try catch as you suggested and it worked. I wish I knew what was going on though and how to fix it properly. Maybe I am not killing the HTTPRequest object correctly??




try{
if(oHTTPPronounceReq.status == 200){
var response = oHTTPPronounceReq.responseText;
}
}catch(e){
//grrrr
}

Sorry, I didn't that last post.

PHP Version 4.4.4

System:
Darwin Test-Servers-Computer.local 7.9.0 Darwin Kernel Version 7.9.0: Wed Mar 30 20:11:17 PST 2005; root:xnu/xnu-517.12.7.obj~1/RELEASE_PPC Power Macintosh

Build Date

Nov 1 2006 18:15:59

Configure Command

'/SourceCache/apache_mod_php/apache_mod_php-17.12/php/configure' '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--with-apxs' '--with-ldap=/usr' '--with-kerberos=/usr' '--enable-cli' '--with-zlib-dir=/usr' '--enable-trans-sid' '--with-xml' '--enable-exif' '--enable-ftp' '--enable-mbstring' '--enable-mbregex' '--enable-dbx' '--enable-sockets' '--with-iodbc=/usr' '--with-curl=/usr' '--with-config-file-path=/etc' '--sysconfdir=/private/etc' '--without-pear'

Server API
Apache

Virtual Directory Support

disabled

Configuration File (php.ini) Path
/private/etc/php.ini

PHP API 20020918

PHP Extension 20020429

Zend Extension 20050606

Debug Build no

Zend Memory Manager enabled

Thread Safety disabled

Registered PHP Streams php, http, ftp, compress.zlib

Apache Version Apache/1.3.33 (Darwin) PHP/4.4.4

Is that enough info?

Lets try something like this and see if it makes any difference.




if(req==null){
req = getXMLHTTPRequest();
}
else if(req){
req.abort();
}
if(req) {
req.open.....);
req.onreadystatechange = processReqChange;
req.send("");
}
else{
alert("The XMLHttpRequest Object is not supported");
}




function processReqChange() {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 0) {
alert(req.responseText);
}
else {
try{alert("There was an issue retrieving the data:\n" +
"Reason: " + req.statusText);
}
catch(e){
alert("There was an issue retrieving the data");
}
}
}
}




One problem with cancelling the request with every keystroke is a fast typer will not see anything until they are done. I normally set a Timeout to wait XXXms and than fire a request off and when the request comes back, if the text is different, I fire off another request.

Eric

lol...I just wanted Apache...With Apache, when you do a post you NEED to set the Content-type request header. Others seem to be more forgiving!

Eric










privacy (GDPR)