JQuery implements the method of checking the user login status after page loading based on ajax, jqueryajax
This example describes how jQuery checks the logon status after loading pages based on ajax. We will share this with you for your reference. The details are as follows:
If a member has logged on to a website with the membership function, the corresponding logon status should be displayed, in addition, this kind of display requirement is available on every page of the website (currently, Chinese websites seem to be doing this, and other forms of Status display methods have not been seen, when opening a new page, you must know whether the member has logged on. You must determine the logon status.
1. solution.
To determine the logon status of a member on each page, I used ajax to pass the parameters on the page to determine the logon status returned by the backend. Of course, the premise of this method is that the logon status can be maintained at the backend or can be queried without sending special parameters to the backend using the page.
2. Code section.
(1) html section
<div id="state_content"></div>
(2) jquery Section
JQuery (document). ready (function () {getUserData () ;}); function getUserData () {var Option = {url: encodeURI ('/Handler/AuthAccounts. ashx? Action = getloginstate '), type: "post", dataType: 'text', cache: false, // setting it to false will not load request information from the browser cache. Async: true, // (default: true). All requests are asynchronous requests. Send a synchronization request. Set this option to false. The browser is locked for Synchronous requests. Other operations can be performed only after the request is completed. Timeout: 150000, // set the request timeout (MS ). This setting overwrites the global setting. Error: function () {}, success: function (data, textStatus) {if (data = null | data = undefined) {return false ;} jsondata = eval ('+ data +'); if (jsondata. state = "success") {var weburl = '<a class = "username"> welcome,' + jsondata. message. split ('|') [1] + '</a> <a class = "go_out" onclick = "ExitLoginState ()"> exit </a> '; $ ("# state_content" ).html (weburl); // content} else {var textList = '<a href = "/Lo Gin/index.shtml "rel =" external nofollow "rel =" external nofollow "> [logon] </a> <a href ="/Register/index.shtml "rel =" external nofollow" rel = "external nofollow"> [registration] </a> '; $ ("# state_content" ).html (textList); // content }}, beforeSend: function () {}}; jQuery. ajax (Option); return false;} function ExitLoginState () {var Option = {url: encodeURI ('/Handler/AuthAccounts. ashx? Action = exitloginstate '), type: "post", dataType: 'text', cache: false, // If set to false, the request information will not be loaded from the browser cache. Async: true, // (default: true). All requests are asynchronous requests. Send a synchronization request. Set this option to false. The browser is locked for Synchronous requests. Other operations can be performed only after the request is completed. Timeout: 150000, // set the request timeout (MS ). This setting overwrites the global setting. Error: function () {}, success: function (data, textStatus) {if (data = null | data = undefined) {return false ;} jsondata = eval ('+ data +'); if (jsondata. state = "success") {alert ("exited "); var textList = '<a href = "/Login/index.shtml" rel = "external nofollow" rel = "external nofollow"> logon </a> <a href = "/ register/index.shtml "rel =" external nofollow "rel =" external nofollow "> [Register] </a> '; $ ("# state_content" ).html (textList); // content }}, beforeSend: function () {}}; jQuery. ajax (Option); return false ;}