struggling with ajax

Hi folks

I have created a customclientraw.txt and its working ok (for this tag anyway lol)
I have a new ajaxWDwx2.js (notice the 2)
I have added this path in the above file var clientrawFile = ‘…/customclientraw.txt’; its in the home dir
I have added this in the above file

		//Snow Height ...
		snowheight = clientraw[1];
		set_ajax_obs("ajaxsnowheight",snowheight);

I have added this to my page

<span class="ajax" id="ajaxsnowheight">Loading</span>

Yet the snow height will not change, i am now baffled, i knew this was gonna be a bad idea :oops:

The ajax script is designed to check the contents of the clientraw.txt file to make sure it has ‘12345’ at start and ‘!!’ at end of record
if not, it does not update.

inside ajaxWDwx2.js change:

	
// now make sure we got the entire clientraw.txt  -- thanks to Johnnywx
	// valid clientraw.txt has '12345' at start and '!!' at end of record
	var wdpattern=/\d+\.\d+.*!!/; // looks for '!!nn.nn!!' version string 
	// If we have a valid clientraw file AND updates is < maxupdates
	if(clientraw[0] == '12345' && wdpattern.test(x.responseText) && 
	    ( updates <= maxupdates || maxupdates > 0  ) )

to:

	
if ( updates <= maxupdates || maxupdates > 0  ) 

Thanks, I have done that but still no update

I take doing the above eliminates the need to have the 12345 and !! in the file?

I actually added those to see what would happen and it stopped the only tag that worked
from working…

yes, it eliminates it

You’ll also need to change the name of the function ajaxLoader to another name (like ajaxLoader2) to prevent interference with the regular AJAX function in ajaxWDwx.js

Hi Ken and thanks

Will i need to change all the instances of ajaxLoader in the ajaxWDwx2 file?

Yes, both the function name itself, and all the function references … otherwise the ‘other’ function will be called instead :slight_smile:

It doesn’t matter that other functions are duplicated, since they provide the same conversions/utility things used by both scripts, but the main script function and it’s invocations have to have a new name to differentiate it from the existing function name.

You can see a (massively complicated) version of the for the ajaxVWSwxf.js script (AJAX for a product-who’s-name-won’t-be-mentioned). That script has two ajaxLoaders, and a one-time loader to parse the units to use from a config file. It does show how to have 3 different ajax functions all coexist.

Best regards,
Ken

Hi Ken

thanks alot, i done all that and its sort of working now, but it looks like its not using the
new customclientraw.txt file, its still using the original one, i must have forgotten something?

I assumed that changing this was all that was needed for the clientraw file?
var clientrawFile = ‘…/customclientraw.txt’;

You probably need to do a global replacement for ‘clientrawFile’ to be ‘clientrawFile2’ in your ajaxWDwx2.js so it doesn’t interfere with the ajaxWDwx.js definition of your ‘real’ clientraw file.

Also, remember to use the Absolute URI for the file name ‘/customclientraw.txt’ (if the file is uploaded by WD to your document root directory).

you mean something like usr/name/…/customclientraw.txt

No… that would only be for PHP (which only knows about filesystem addresses).
The URI is the path part of the URL which the webserver uses… JavaScript only knows about website addresses, not filesystem addresses.

So ‘/customclientraw.txt’ in a JavaScript means the ‘customclientraw.txt’ file located in the document root of the website
i.e. http://www.northantsweather.com/customclientraw.txt

Best regards,
Ken

Hi Ken,

Sorry but i am really struggling now, do i put …/customclientraw.txt or do i put
http://www.northantsweather.com/customclientraw.txt

I think there both the same as they both find clientraw #1 anyway,

As for changing clientrawFile to clientrawFile2 has left the site with a wee issue
#1 its slow loading now and #2, well see for yourself, the ajax has a problem lol
http://www.northantsweather.com/weather/wxindex.php

In the ajaxWDwx2.js, just put

var clientrawFile2 = ‘/customclientraw.txt’;

That should work irrespective of what page loads it.

in your ajaxLoader2 function, you need to delete ALL the old function contents, and add in your new contents to decode the customclientraw.txt file. Also, you need to delete

//element = document.getElementById(“ajaxcounter”);

//if (element) {

window.setInterval(“ajax_countup()”, 1000); // run the counter for seconds since update

//}

in ajaxWDwx2.js … otherwise your counter will be wonky (ajaxWDwx.js already doing the counter).

OK i had a feeling you would ask me to delete them all, to be honest i aint got
clue what to remove and what needs to stay

I am thinking anything with = clientraw[xx] needs to go?

You can keep

// ------------------------------------------------------------------------------------------
//  main function.. read clientraw.txt and format <span class="ajax" id="ajax..."></span> areas
// ------------------------------------------------------------------------------------------
function ajaxLoader2(url) {
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
  }
  if (x) { // got something back
    x.onreadystatechange = function() {
    try { if (x.readyState == 4 && x.status == 200) { // Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE 
    var clientraw = x.responseText.split(' ');
if ( updates <= maxupdates || maxupdates > 0  )  {
		if (maxupdates > 0 ) {updates++; } // increment counter if needed


Delete everything after that down to

 	  } // END if(clientraw[0] = '12345' and '!!' at end)

	 } // END if (x.readyState == 4 && x.status == 200)

    } // END try

   	catch(e){}  // Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE

    } // END x.onreadystatechange = function() {
    x.open("GET", url, true);
    x.send(null);


and add your new code in the space between the two code snippets.

You should also remove

//  reset the flash colors, and restart the update unless maxupdate limit is reached

    setTimeout("reset_ajax_color('')",flashtime); // change text back to default color 


(since the ajaxWDwx.js will do that already)

and remove

//element = document.getElementById("ajaxcounter");
//if (element) {
  window.setInterval("ajax_countup()", 1000); // run the counter for seconds since update
//}

for the same reason.

Thanks again Ken, getting much closer now, well, the sites speeded up again and
i can now see some data, although, its not quite what i was expecting, its showing

Snow height as 341m but it should be at 1120m thats what the tag says in wd

I did pinch the cloud height code from the other ajax file and just changed the variables

But i have a feeling it thinks its in feet and its converting it to meters, does that sound
about right?

This is the code i have for it…

		// Snowheight
		snowheight = clientraw[0];
		set_ajax_obs("ajaxsnowheight", convertHeight(snowheight) + uomHeight);

Hi Ken,

I finally got it sorted using a copy of the wetbulb setup instead of using the cloud height 1

	//snowheight ...
	snowheight = clientraw[0];
	set_ajax_obs("ajaxsnowheight",snowheight);

Thanks so much for helping me out…
Also thanks to everyone else that set me off in the right direction
I now have a base to work from, i now just need to get it all working
in the customclientraw file :slight_smile:

Thanks again guys!!