Home  >  Article  >  Web Front-end  >  Prevent post requests in ajax in page url cache

Prevent post requests in ajax in page url cache

小云云
小云云Original
2018-01-25 13:07:321141browse

This article mainly introduces the relevant information on how to handle post requests in ajax to prevent page URL caching. I hope this article can help everyone. Friends in need can refer to it. I hope it can help everyone.

Preventing the processing of post requests in ajax in the page URL cache

Generally, we often use Ajax requests in development, send requests asynchronously, and then To get the data we want, using Get to request data in Ajax will not cause page caching problems, but when using POST requests, sometimes the page will cache the information we submit, causing the asynchronous requests we send not to correctly return what we want. data, so what should we do when encountering this situation? ? ?

Here is a way to prevent post request page caching url information in ajax:


$.post(url,data ,ranNum:Math.random()} ,function(data){    
      if("success"==data){ 
      alert("success"); 
        }else{ 
          alert("error"); 
        } 
    })


url : 请求的URL 地址 

data : 请求的数据 

ranNum : 这个是防止缓存的核心,每次发起请求都会用Math.random()方法生成一个随机的数字,这样子就会刷新url缓存

There are many ways to generate this ranNum. You can also generate it according to your own generation method. Just make sure that the data is generated randomly! This is how Ajax prevents url caching when sending requests.

Related recommendations:

Two methods for javascript to parse url into json format

Some notes on JS jumping mobile site url Detailed explanation of matter examples

Detailed explanation of examples Analysis of url object management in js

The above is the detailed content of Prevent post requests in ajax in page url cache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn