Click on login to refresh the page

Keywords: Web Development JSON

Click events are added to login, but the page will be refreshed when jumping, but it is possible to click Enter.

  • Among them, the landing tag reads like this
<a class="lgBut" id="btn_login" href="" >Sign in</a>
  • This is the way below.
   $("btn_login").click(function(){
	    var USERNAME=$("#USERNAME").val();
	    var PASSWORD=$("#PASSWORD").val();
	    $.ajax({
	        type:'get',
	        url:  address + "/appchange/appLogin?"+Math.random(),
	        data: {
	            USERNAME: USERNAME, PASSWORD: PASSWORD
	        },
	        xhrFields: {
	            withCredentials: true
	        },
	        crossDomain: true,
	        cache:false,
	        dataType:'json',
	        success:function(data){
	            if (data.info == "success") {
	                window.localStorage.errortimes=0;
	                window.localStorage.userinfo=JSON.stringify(data.USER);
	                window.localStorage.USER_CODE=data.USER.USER_CODE;      					if(window.localStorage.href==undefined||window.localStorage.href=="")
	                {
	                    window.location.href="index.html";
	                }
	                else{
	                    window.location.href=window.localStorage.href;
	                    window.localStorage.href="";
	                }
	                window.localStorage.logintime=new Date();
	            }
	            else{
	            	 layer.msg(data.info);
	                window.localStorage.errortimes++;
	            }
	        },
	        error:function(ex){
				
	        }
	    });
	});
	$(document).keyup(function(event){
        if(event.keyCode ==13){
           login();
        }
    });

Later, it was found that the href in the login tag played a role, and that the $("btn_login").click(function(){} event would execute everything in the a tag, while when returning, only one click {} method was executed. If href = ", the href was executed when ajax was executed, and the page was refreshed before ajax was executed, so the page would be refreshed.

Posted by beginPHP on Tue, 29 Jan 2019 22:00:15 -0800