$ .ajax (), only when updating

I am reading xml using the $ .ajax () request.

$.ajax({
    type: "GET",
    async: false,
    url: "../../../ErrorMessages.xml",
    dataType: "xml",
    success: function (xml) {
       $(xml).find("*[Name='" + Field + "']").each(function () {
          message = $(this).find(Rule).text();
       });
    }
});

I want to make a call only when the ErrorMessages.xml resource is updated . otherwise use the browser cache.

+3
source share
3 answers

The browser will not know if it is updated ErrorMessages.xmlon the server. It should issue a request to check if the file has been modified.

You can set the option ifModifiedto truein the jQuery query $.ajax(), since this is the default value false:

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

Quote from jQuery.ajax () documentation :

ifModified (Boolean)

Default: false

, . Last-Modified. - false, . jQuery 1.4 "etag", , .

- Last-Modified, XML :

GET /ErrorMessages.xml HTTP/1.1
Host: www.example.com

HTTP/1.1 200 OK
Last-Modified: Wed, 06 Oct 2010 08:20:58 GMT
Content-Length: 1234

:

GET /ErrorMessages.xml HTTP/1.1
Host: www.example.com
If-Modified-Since: Wed, 06 Oct 2010 08:20:58 GMT

HTTP/1.1 304 Not Modified

- , If-Modified-Since, .

+3

var "updateTimeStamp" updateatimestamp xml. , ajax ipdateTimeStamp. , , ,

0

Take a look here: http://jquery14.com/day-01/jquery-14

Etag support was included in jQ 1.4, so you can use it.

0
source

Source: https://habr.com/ru/post/1768189/


All Articles