Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-13-2007, 05:25 PM   PM User | #1
MikoLone
Regular Coder

 
Join Date: Jun 2002
Location: Provo, UT
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
MikoLone is an unknown quantity at this point
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.

Code:
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 try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?
MikoLone is offline   Reply With Quote
Old 03-13-2007, 06:18 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
I explain the error message here:

http://radio.javaranch.com/pascarell...345471027.html

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

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 03-13-2007, 06:46 PM   PM User | #3
MikoLone
Regular Coder

 
Join Date: Jun 2002
Location: Provo, UT
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
MikoLone is an unknown quantity at this point
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
__________________
I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?
MikoLone is offline   Reply With Quote
Old 03-13-2007, 07:00 PM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Can I ask what is the server enviornment you are running this on?

Thanks,
Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 03-13-2007, 07:06 PM   PM User | #5
MikoLone
Regular Coder

 
Join Date: Jun 2002
Location: Provo, UT
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
MikoLone is an unknown quantity at this point
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??


Code:
try{
  if(oHTTPPronounceReq.status == 200){
	var response = oHTTPPronounceReq.responseText;
  }
}catch(e){
	//grrrr
}
__________________
I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?
MikoLone is offline   Reply With Quote
Old 03-13-2007, 07:24 PM   PM User | #6
MikoLone
Regular Coder

 
Join Date: Jun 2002
Location: Provo, UT
Posts: 142
Thanks: 0
Thanked 0 Times in 0 Posts
MikoLone is an unknown quantity at this point
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?
__________________
I try to convince 'em that I am computer geek, but I just can't do it. Why? Oh why?
MikoLone is offline   Reply With Quote
Old 03-13-2007, 07:26 PM   PM User | #7
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Lets try something like this and see if it makes any difference.


Code:
        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");
        }
Code:
      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
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 03-13-2007, 07:27 PM   PM User | #8
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
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
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 AM.


Advertisement
Log in to turn off these ads.