AJAX getting wrong page?

slightlyoff
slightlyoff used Ask the Experts™
on
I've never head of this happening before - I'm not quite sure exactly what is going on.  Unfortunately the page this is occurring on is an "Admin" page - and I can't provide a useful link for you to see the behavior.  I'll do my best to describe it.

I have a page that allows the user to put online items that are in stock.  When a user enters a SKU, using AJAX, i return a HTML form  generated and filled in from the DB by "hrgetSku.asp".  The user can also click on a list of products already in the "in-stock" list by clicking a link in a list.

My XML Code looks like this (it's old code, been working for the past several years...):

<script type="text/javascript">
function getBP()
{

var xmlhttp;
var sku;

sku = document.getElementById('theSKU').value;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","hrgetSku.asp?sku="+sku,true);
xmlhttp.send();

return false;
}

function getEBP(aSku)
{

var xmlhttp;
var sku;

sku = aSku;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
//alert("here!");
xmlhttp.open("GET","hrgetSku.asp?sku="+sku,true);
xmlhttp.send();

return false;
}

Open in new window


When you click a link, you call the function like this:  <a onclick="getEBP('x4tte'); return false;" href="#">View x4tte</a>

You'll notice at the end of the getEBP() function there's an "alert('here')" commented out for testing purposes.
When I run my code (and the alert is uncommented out) - every link I click on works - I get the alert.  HOWEVER....

Some products seem to be pulling from an old or different "hrgetSku.asp" page.  I've made changes to "hrgetSku.asp" posted it, and for some products I see the change and for others i don't see the change.  As I mentioned earlier, "hrgetSKU.asp" returns a form that is filled in with the database.  To make sure the right page was getting called, I changed some titles on the form.  On most products the changes show up. On some, however, they don't - even though they are running through the same ajax and calling the same page.  Further - those products that don't show up don't pull all the data.  It's as if they are pulling a different hrgetSKU.asp page all together.  They are pulling some version of it, because it looks identical, minus my changes and missing data.

So this is very strange...  I even added the full "http://www.mywebsitehere.com/admin/hrgetSKU.asp" path to the hrgetSKU.asp page in my ajax code.  It still doesn't pull the file  with my changes.  I think Im going mad...

This is a difficult question to ask since I can't show you the behavior, but  I hope I've made clear what's going on.
Any help would be greatly appreciated!!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web Ninja at large
Commented:
there could be an issue with caching that's occurring, to avoid any caching issues, you need to send an original url everytime. The best way to achieve this is to add another querystring parameter that contains a random value:

xmlhttp.open("GET","hrgetSku.asp?sku="+sku,true);  

can be changed to:
var randNum = Math.round(Math.random()*10000);
xmlhttp.open("GET","hrgetSku.asp?sku="+sku + "&r=" + randNum,true);
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
Placing a unique url as big montey shows is one thing but your ajax pages should also be in a different folder.

The only thing I would suggest to change for the url is not use a random number because randomness does not mean uniqueness.  

var dt = Date.now();
xmlhttp.open("GET","another_folder/hrgetSku.asp?sku="+sku + "&r=" + dt,true);
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
Also, all the browsers have a console feature.  https://developer.chrome.com/devtools/docs/console  In chrome, if you run your page with the console open, you can look at what is going on with the ajax page, what data is being sent and what is being returned.  To see this info, click on the Network tab and find the ajax page, then click on it. Then on the right there will be some options to see the request and response.

Try and run the page directly as well from a url.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Big MontyWeb Ninja at large

Commented:
There's no reason to put your Ajax pages in a separate folder other then organizational reasons for yourself. A url is a url is a url.

As for a random number vs using a date, chances of repeating a random number twice in a row (and therefore caching the url) is so unlikely to happen it's not something to worry about until you figure out the cause of your issue. I've used both methods throughout the year and never into any caching problems.

Author

Commented:
Thanks all for the responses. I'm just getting back into the office today and will look into it more.
I really appreciate the help!

Author

Commented:
Thanks for your help on this.
I came in this morning, and everything worked as it was supposed to - before I put the random number on.
I assume that's because over the weekend something refreshed on the server.  So I think you're right, it's a caching issue.

I've added the random numbers and tested it.  I'll have to wait and see if it happens again.

Thanks for your help!  I was worried it was such a weird question that no one would answer.
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
Big Monty - Different folders are not just for keeping things organized. https://support.microsoft.com/en-us/kb/290591NOTE: The two ASPs should be in different virtual folders due to threading issues..  That type of note is on many xmlhttp help files.  I have always used that for both server side and client side ajax.  

As for randomness, check out the collisions http://jsbin.com/movuje/1/edit?html,output. If it takes the same effort to just use the time which will not be duplicated, why wouldn't you use it?
Big MontyWeb Ninja at large

Commented:
interesting article, but it looks to be discussing Windows 2000.

do you have any documentation for newer versions of IIS, as I would be curious about it...
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
It's always been that way for classic asp as far as I can remember.  It can still physically work in the same folder, but when I have had issues this is what it always came back to and of course with client side generating a unique something to add to the url.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial