Skip to content Skip to sidebar Skip to footer

$.ajax() Call, Only On Updation

I am reading a xml using $.ajax() request. $.ajax({ type: 'GET', async: false, url: '../../../ErrorMessages.xml', dataType: 'xml', success: function (xml) {

Solution 1:

The browser won't know if ErrorMessages.xml has been updated on the server. It has to issue a request to check if the file has been modified.

You may want to set the ifModified option to true in your jQuery $.ajax() request, since this is set to false by default:

$.ajax({
  type: "GET",
  ifModified: true,
  async: false,
  url: "../../../ErrorMessages.xml",
  dataType: "xml",
  success: function (xml) {
     // ..
  }
});

Quoting from the jQuery.ajax() documentation:

ifModified (Boolean)

Default: false

Allow the request to be successful only if the response has changed since the last request. This is done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery 1.4 this technique also checks the 'etag' specified by the server to catch unmodified data.

As long as your web server supports the Last-Modified header, then the first request to the XML would look something like this:

GET/ErrorMessages.xmlHTTP/1.1Host:www.example.comHTTP/1.1200OKLast-Modified:Wed,06Oct2010 08:20:58 GMTContent-Length:1234

However, subsequent requests to the same resource will look like this:

GET/ErrorMessages.xmlHTTP/1.1Host:www.example.comIf-Modified-Since:Wed,06Oct2010 08:20:58 GMTHTTP/1.1304NotModified

If the web server finds that the file has been modified since the If-Modified-Since header date, it will serve the file normally.

Solution 2:

make a global var named "updateTimeStamp" and put the updatetimestamp into the xml. then find the timestamp when you do the ajax request and compare it to your saved ipdateTimeStamp. if it is bigger then do what you need to do and if not do nothing

Solution 3:

Look here: http://jquery14.com/day-01/jquery-14 Etag support was enabled in jQ 1.4, so you could use it.

Post a Comment for "$.ajax() Call, Only On Updation"