AJAX Demo from Syncor needed modification

Discussion to talk about software related topics only.
Post Reply
khoney
Posts: 125
Joined: Fri Sep 11, 2009 12:43 pm

AJAX Demo from Syncor needed modification

Post by khoney »

FYI,

I tried to run the AJAX demo that was on the embeddedethernet wiki. It did not work correctly for me - the tick count would update only when the browser first loaded the main web page. Even the F5 would not cause updates. After some debugging, I determined that IE was caching the GET requests for the ajax.htm file. To get around this, I had to create a unique request by getting "ajax.htm?RQ=reqnum", where reqnum is a variable I increment each time I do a get. Now the updates work properly. I thought I'd post this in case anyone is having problems with the demo.

I would have posted to the AJAX thread in the Application Notes and Design Ideas section, but that appears to be locked down.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX Demo from Syncor needed modification

Post by tod »

The demo is pretty old at this point and I've really been meaning to update it. In my real code I use the Prototype library for making Ajax requests and I add the parameter
requestHeaders:["If-Modified-Since","Sat, 1 Jan 2000 00:00:00 GMT"],
to the call to prevent IE from caching. I used to do something similar to what you suggest but instead of tracking a number I would just add
&nocache="+Math.random();
to the parameter list. This unfortunately has the side effect of filling the cache with requests. So the requestHeaders is the better way to go. Here's an excerpt from my library in case it helps anyone. (syn is just a javascript object that holds all my generically useful javascript code - so you'll need to remove that as well as the call to syn.Debug

Code: Select all

//==============================================================================
// Ajax code using the prototype library. This is the only method needed
// for doing Ajax calls. The Ajax object and specifically the Ajax.Request
// method allows us to specify both the request and the function to be called
// on completion. I wrote this wrapper so that the completion function could
// be passed in and therefore used by all requests in the entire system. 
//==============================================================================
syn.MakeAjaxRequest = function(theParams, completionFunction, callingScope)
{
	try
	{
		//If the completionFunction needs to know the calling scope (i.e. what was "this"
		// when the request was issued, then the request passed the calling scope into
		// this method. 
		var pass_scope = false;
		if (arguments[2])
		{
			pass_scope = true;
		}

		new Ajax.Request("ajax.htm",
								{	method:"get",
									parameters:theParams,
									//THE NEXT LINE is the  solution to IE caching ajax requests
									requestHeaders:["If-Modified-Since","Sat, 1 Jan 2000 00:00:00 GMT"], 			
									onComplete: function (transport)
									{
										if (pass_scope) 
										{
											completionFunction.call(callingScope,transport.responseText);
										}
										else completionFunction(transport.responseText);
									}
									
								}); //end of new Ajax.Request
	}
	catch (exception)
	{
		syn.Debug("syn.MakeAjaxRequest: "+exception.message+" ");
	}           			               
};
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX Demo from Syncor needed modification

Post by tod »

I finally did the update and posted it. http://forum.embeddedethernet.com/viewt ... ?f=7&t=548

Tod
Syncor Systems, Inc.
Post Reply