Setting the cache in jQuery $.post() to be false?

前端 未结 4 499
难免孤独
难免孤独 2020-12-09 12:48

I have this code:

$.post(\"php/tagNotifUnsub.php\", $(\"#tagNotifUnsub\").serialize(), function(){ 
            $(\'#tagSubDiv\').load(\'tags.php #tagSubDiv\         


        
相关标签:
4条回答
  • 2020-12-09 12:58

    $.post is not cached.

    Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

    From http://api.jquery.com/jQuery.post/

    0 讨论(0)
  • 2020-12-09 13:03

    Partly right, Daniel A. White!

    On devices running iOS6 and Safari, POST is also cached.

    What you can do is the following:

    $.ajaxSetup({
       type: 'POST',
       headers: { "cache-control": "no-cache" }
    });
    
    0 讨论(0)
  • 2020-12-09 13:04

    This is an old problem that seems to pop up from time to time and I am always scratching my head. This solution fixes the problem but it may not be suitable in all cases as it is in effect disabling a key feature of AJAX - The asynchronous feature. That said and done for the cases where I have had this problem - Apple products and Safari I'm looking at you especially - setting the additional parameter of async:false in your jQuery AJAX request fixes the issue and actually solves the "No Alert on Post Callback" problem.

    As I said if you absolutely rely on asynchronous functionality then this is not going to help you, but for many projects it will stop you pulling your hair out.

    $.ajax({
      type: 'POST',
      url: url,
      data: data,
      success: success,
      dataType: dataType,
      async:false
    });
    
    0 讨论(0)
  • 2020-12-09 13:14

    You mention $.ajax

    You can turn caching off for ajax like this:

    $.ajax({
            url: "/myURL?",
            cache: false,
        });
    
    0 讨论(0)
提交回复
热议问题