Eric - Have a look at some of the ajax functions I wronte for VuFind - there are some almost identical function calls that work just fine.
http://vufind.svn.sourceforge.net/viewvc/*checkout*/vufind/web/services/Record/ajax.js?revision=106
See function SaveTag
Also - You might want to consider using the Yahoo YUI Connection Manager or the Prototype AJAX toolkit. They both work great and you don't need to spend time debugging. I also find firebug (firefox plugin) to be an awesome ajax debugger.
Just by looking at your function real quick - you are calling httpRequest.send('') at the end of your function. I think I read somewhere that you should send null and not an empty string. Maybe that will solve it? Not really sure.
Andrew
> -----Original Message-----
> From: Code for Libraries [mailto:[log in to unmask]] On Behalf Of
> Eric Lease Morgan
> Sent: Thursday, November 29, 2007 9:22 AM
> To: [log in to unmask]
> Subject: [CODE4LIB] httpRequest javascript.... grrr
>
> Why doesn't my httpRequest Javascript function return unless I add an
> alert? Grrr.
>
> I am writing my first AJAX-y function called add_tag. This is how it
> is suppose to work:
>
> 1. define a username
> 2. create an httpRequest object
> 3. define what it is suppose to happen when it gets a response
> 4. open a connection to the server
> 5. send the request
>
> When the response it is complete is simply echos the username. I know
> the remote CGI script works because the following URL works correctly:
>
> http://mylibrary.library.nd.edu/demos/tagging/?
> cmd=add_tag&username=fkilgour
>
> My Javascript is below, and it works IF I retain the "alert
> ( 'Grrr!' )" line. Once I take the alert out of the picture I get a
> Javascript error "xmldoc has no properties". Here's my code:
>
>
> function add_tag() {
>
> // define username
> var username = 'fkilgour';
>
> // create an httpRequest
> var httpRequest;
> if ( window.XMLHttpRequest ) { httpRequest = new XMLHttpRequest();
> }
> else if ( window.ActiveXObject ) { httpRequest = new ActiveXObject
> ( "Microsoft.XMLHTTP" ); }
>
> // give the httpRequest some characteristics and send it off
> httpRequest.onreadystatechange = function() {
>
> if ( httpRequest.readyState == 4 ) {
>
> var xmldoc = httpRequest.responseXML;
> var root_node = xmldoc.getElementsByTagName( 'root' ).item( 0 );
> alert ( root_node.firstChild.data );
>
> }
>
> };
>
> httpRequest.open( 'GET', './index.cgi?cmd=add_tag&username=' +
> username, true );
> httpRequest.send( '' );
> alert ( 'Grrr!' );
>
> }
>
>
> What am I doing wrong? Why do I seem to need a pause at the end of my
> add_tag function? I know the anonymous function -- function() -- is
> getting executed because I can insert other httpRequest.readyState
> checks into the function and they return. Grrr.
>
> --
> Eric Lease Morgan
> University Libraries of Notre Dame
>
> (574) 631-8604
|