How to detect when XHR returns a cached resource?

前端 未结 3 2080
难免孤独
难免孤独 2020-12-16 15:57

I\'m wondering if there is a way how to detect when a response is returned from a local cache? Is it possible?

The solution should be general and work for unconditi

3条回答
  •  天涯浪人
    2020-12-16 16:12

    Check to see if the status code returned is 304 (not modified) in the onreadystatechange function. Something along the lines of:

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==304)
        {
          alert("Cached");
        }
    } 
    

提交回复
热议问题