0

Working on classifieds website project and using and open source framework "osclass", i'm trying to load Every category count with AJAX api call, but sometimes it do not return accurate results

Example: Total active cars in database: 71 but API returns 68

after refreshing the chrome cache i get exact number:

here is my simple API code:

$result = $con->query("SELECT pk_i_id FROM oc_t_item where pk_i_id in (select fk_i_item_id from oc_t_item_location where fk_i_region_id=$id) and fk_i_category_id=$cat  and dt_expiration >= now() and b_enabled=1 and b_active=1") or die(mysql_error());
      $count=  mysqli_num_rows($result);
      $array = mysqli_fetch_row($result);  
      }
      else{
         $result = $con->query("SELECT pk_i_id FROM oc_t_item where  fk_i_category_id=$cat  and dt_expiration >= now() and b_enabled=1 and b_active=1") or die(mysql_error());
         $count=  mysqli_num_rows($result);enter code here
         $array = mysqli_fetch_row($result);  
      }   
echo json_encode($count);

I think its cache related issue but i dont know how to deal with that issue, Help would be appreciated!

  • Just the heads up. It looks like you are updating database/status of the application with GET requests. This is why the problem is occurring. Have a read: http://stackoverflow.com/questions/4573305/rest-api-why-use-put-delete-post-get – Dawid O Nov 06 '14 at 08:44
  • thanks for reply, i read that post but in my case ..i didn't use any API for updating status in database, using it only to read category stats from database – khalid umar Nov 06 '14 at 10:45
  • I am saying in general. The only reason the browser returns the same result is because it is cached (like you said yourself). If there is no POST/PUT/DELETE between two GET requests the server is SUPPOSE to return the same answer. – Dawid O Nov 06 '14 at 10:47
  • Right!, Thanks for explaining, issue is solved now :) – khalid umar Nov 06 '14 at 12:08

2 Answers2

2

When call ajax request to server add to parameters any unique key generated by random or just time.

For example

url = 'getData.php?unique=' + Math.random();
newman
  • 2,689
  • 15
  • 23
  • can you please little elaborate it, why to put unique random number in request parameters ? – khalid umar Nov 06 '14 at 10:41
  • Browser store in cache some results for similar url. If you make some request to one URL to browser can use value from cache and not make real request to server. When you add unique params then you tell browser make request to server for each call. – newman Nov 06 '14 at 10:55
0

Just like I explained in the comments it looks like the state of the application changes between two GET requests. This isn't suppose to happen: REST API - why use PUT DELETE POST GET?

You always make some sort of request to the server, in case of websites it is GET and sometimes POST (when you send data via forms).

@newman answser is a good one. You have to make a different request, so that the server/browser doesn't return already precomputed and cached value for that URL, but actually evales the request.

You can also use timestamp (in javascript in miliseconds) which is guaranteed to return always unique number:

url = 'getData.php?unique=' + new Date().getTime();
Community
  • 1
  • 1
Dawid O
  • 6,091
  • 7
  • 28
  • 36