zoukankan      html  css  js  c++  java
  • Ajax缓存的几种处理方式

    当ajax请求的接口如果没有发生变化的情况下,那么会缓存中读取数据,
    不会再向服务器请求数据,如果接口的地址发生了改变那么会再次向服务器请求数据
     
    1.  通过URL添加后缀的方式
     
    本来请求的地址是: /home/action?
    
    加查询参数后缀后:/home/action?ran=Match.Random();
    

      

    2. 通过Jquery的Ajax  API设置相关属性(代码中标红处)

    <script type="text/javascript">
            var LoadTime = function () {
                $.ajaxSetup({ cache: false });
                $.ajax({
                    url: '@Url.Action("currTime")',
                    success: function (result) {
                        $("#currTime").html(result);
                    }
                })
            }
    </script>
    

    附带

    AJAX的封装

    function ajax(options){
    	//创建一个ajax对象
    	var xhr = new XMLHttpRequest() || new ActiveXObject("Microsoft,XMLHTTP");
    
    	//数据的处理  {a:1,b:2}  a=1&b=2;
    	var str = "";
    	for(var key in options.data){
    		str+="&"+key+"="+options.data[key];
    	}
    
    	str = str.slice(1)
    	if(options.type == "get"){
    		var url = options.url+"?"+str;
    
    		xhr.open("get",url);
    
    		xhr.send();
    
    	}else{
    		xhr.open("post",options.url);
    
    		xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
    
    		xhr.send(str)
    
    	}
    
    	//监听
    	xhr.onreadystatechange = function(){
    		//当请求成功的时候
    		if(xhr.readyState == 4 && xhr.status == 200){
    
    			var d = xhr.responseText;
    
    			//将请求的数据传递给成功回调函数
    			options.success&&options.success(d)
    
    		}else if(xhr.status != 200){
    			//当失败的时候将服务器的状态传递给失败的回调函数
    			options.error&&options.error(xhr.status);
    		}
    	}
    }
    

      

  • 相关阅读:
    Spring Boot:自动配置原理
    centos7安装zookeeper
    spring集成RabbitMQ
    centos7安装RabbitMQ
    centos7安装MongoDB4.0(yum安装)
    nginx日志分割
    centos7 yum安装nginx
    centos7使用cronolog分割tomcat8.5的catalina.out日志
    ubuntu18安装微信
    idea打开dashboard
  • 原文地址:https://www.cnblogs.com/moonzwt/p/9562254.html
Copyright © 2011-2022 走看看