jqueryajaxhttp-status-code-304

ajax 304 not modified can't receive response


I have an API to handle cache control. Server will set response header "last-modified" and next request from browser will have If-modified-Since header.

However, when I use ajax to request API, it will not set If-modified-Since header automatically. And I set If-modified-Since in ajax request header, server response 304 status code. But response body is empty.

Is ajax need to manually to fulfill browser behavior about cache control?

Is there better solution?

I find a similar question here.


Solution

  • Finally, it resolved after I study this.

    Setting Request Header to

    'Cache-Control': 'max-age=0'
    

    If response provide Last-Modified header, next request will automatically set If-Modified-Since and value is equal.

    When 304 response occur, response content is from browser cache.