Home > Web Front-end > JS Tutorial > body text

How to deal with data after Ajax lost refresh

php中世界最好的语言
Release: 2018-04-25 15:04:17
Original
1622 people have browsed it

This time I will show you how to deal with the data after Ajax is lost and refreshed. What are the precautions when dealing with the data after Ajax is lost and refreshed. The following is a practical case, let's take a look.

Ajax Introduction:

AJAX stands for "Asynchronous Javascript And XML" (Asynchronous JavaScript and XML), which refers to A web development technology for creating interactive web applications.

AJAX = Asynchronous JavaScript and XML (a subset of Standard Universal Markup Language).

AJAX is a technology for creating fast, dynamic web pages.

By exchanging a small amount of data with the server in the background, AJAX can enable asynchronous updates of web pages. This means that parts of a web page can be updated without reloading the entire page.

Traditional web pages (not using AJAX) must reload the entire web page if the content needs to be updated.

There is a problem

If you use a browser such as Firefox to access the RMS website, we may find that switching between pages is asynchronous through AJAX The request is implemented, and the URL of the page will not change at the same time. Although the rollback refresh can be implemented through the AJAX asynchronous request through the button on the page, the browser cannot support forward and backward. Every time after refreshing and backing, the page will return to The initial welcome page. AJAX can realize partial refresh of the page, can achieve very good data loading effect, and bring a very good experience to the user. However, AJAX cannot retain records in the browser's historical session. When you click on a page, AJAX various Data loading is very fast. For example, a list page can be turned through asynchronous loading. However, if the user accidentally refreshes the page, the page number will have to be calculated again. Once the user changes the session state (browser forward, backward, refresh) ), then how to deal with data after Ajax loses refresh

Traditional AJAX has the following problems:

1. Page content can be changed without refreshing , but the page URL cannot be changed

2. Direct access to a certain module of the system through the URL cannot be supported, and a click operation must be performed

3. The developer must first go back and refresh. It not only increases the workload for developers, but also does not conform to user habits

4. Furthermore, browsers have introduced the onhashchange interface. Browsers that do not support it can only periodically determine whether the hash has changed

5. But this method is very unfriendly to search engines

Using technology

In order to solve the problems caused by traditional ajax, a new API has been introduced in HTML5, namely :history.pushState, history.replaceState

You can operate the browser history through the pushState and replaceState interfaces and change the URL of the current page.

pushState is to add the specified URL to the browser history, and replaceState is to replace the current URL with the specified URL.

history.pushState(state, title, url)
Copy after login

Add the current URL and history.state to history, and add the new state and URL to the current. It will not cause the page to refresh.

state: State information corresponding to the URL to be jumped to.

title: Title (now ignored and not processed).

url: URL address to jump to, cannot cross domain.

history.replaceState(state, title, url)
Copy after login

The history.replaceState() operation is similar to history.pushState(), except that the replaceState() method modifies the current history entry rather than creating a new entry.

state: State information corresponding to the URL to be jumped to.

title: Title (now ignored and not processed).

url: URL address to jump to, cannot cross domain.

addEventListener(type, listener)
addEventListener is a function that listens to events and processes the corresponding.

type: Type of event.

listener: A function that handles events after listening to them. This function must accept an Event object as its only parameter and cannot return any results.

Solution

由于AJAX无刷新改变页面内容的,所以页面的URL始终是不变的,为了区分页面上的各个不同内容,首先需要重新定义一下各个页面的URL,因为RMS网站多使用$.post异步请求,我们可以用URL记录post请求的各个参数(请求地址、传递参数),当浏览器进行刷新、回退操作时,根据URL记录的信息自动发送post请求,进入对应页面,从而实现希望的功能。

定义URL语法:

已如下地址为例:

“http://localhost/rms_hold/index.php/Home/Index/loadHomePage#/rms_hold/index.php/Home/ResourceRequest/getRequestPage@apply_type=1&resource_name=ADM_BIZCARD!1”

“http://localhost/rms_hold/index.php/Home/Index/loadHomePage”是原先页面的URL,如果在问题解决之前在RMS网站上进行任何点按操作,网址一直不会有任何变动。现在我们使用“#”分割网址,“#”之后就是我们所记录的ajax请求“/rms_hold/index.php/Home/ResourceRequest/getRequestPage”是请求的地址,它由“#”与“@”分割,而在“@”与“!”之间的这是发向请求地址的各个参数,“apply_type=1”与“resource_name=ADM_BIZCARD”由“&”进行分割。

刷新、回退监听处理:

if (history.pushState) {
window.addEventListener("popstate", function() {
back_ajax_mod_url();
back_ajax_post();
if(location.href.indexOf("#")==-1){
window.location.reload();
}
});
back_ajax_mod_url();
back_ajax_post();
}
Copy after login

如以上代码所示,window对象上提供了onpopstate事件,可以使用addEventListener方法监听onpopstate事件,每当URL因为浏览器回退时都会对得到的URL在back_ajax_mod_url()与back_ajax_post()函数中进行解析、处理,而当浏览器刷新时,根据history.pushState的返回值不空,依然会对得到的URL在back_ajax_mod_url()与back_ajax_post()函数中进行解析、处理。

对外接口:

function back_ajax_mod_url(){
var url_ajax=ajaxback_url.pop();
var title ="Home | UniqueSoft RMS";
if(url_ajax){
history.pushState({ title: title }, title,location.href.split("#")[0] + "#"+ url_ajax);
}
}
Copy after login

介绍一下back_ajax_mod_url()函数,它与数组ajaxback_url组成对外接口,ajaxback_url是一个全局数组,用来存放需要加入到history中的URL,然后由back_ajax_mod_url()函数在无页面刷新的情况下将当前URL和history.state加入到history中。

$("#reportTable tbody").on("click", "trtd img[alt = 'Detail']",
function() {
var id = $(this).attr("business_leave_id");
$.post("MODULE/ReportCenter/getReportDetailPage",{
"report_name": "ADM_TRAVEL_REP",
"item_id": id,
},
function(data) {
ajaxback_url.push("MODULE/ReportCenter/getReportDetailPage"+ "@" + "item_id=" + id + "&" +"report_name=ADM_TRAVEL_REP");
$("#container").html(data);
back_ajax_mod_url();
});
});
Copy after login

以上函数是RMS系统里的一个AJAX异步请求事件,会造成页面无刷新变化,加粗部分就是我们提供的对外接口,使用该接口后在history中会产生一条新的URL用来记录达到该页面的post方法。

URL解析处理器:

如下面函数所示back_ajax_post()为RMS系统的URL解析处理器,根据之前提到的URL语法,读出页面上改变内容的AJAX请求,并且自动发送AJAX请求,获取需要的页面

function back_ajax_post() {
if (location.href.indexOf("#")!= -1) {
var post_href =location.href.split("#")[1];
if (location.href.indexOf("@")!= -1) {
var post_url =post_href.split("@")[0];
var post_params =post_href.split("@")[1];
if(post_params.indexOf("!") != -1) {
var post_page_index =post_params.split("!")[1];
post_params =post_params.split("!")[0];
};
} else {
var post_url = post_href;
var post_params = "";
var post_page_index = "";
}
var get_resource_href =location.href;
if(get_resource_href.indexOf("!") != -1) {
get_resource_href =get_resource_href.split("!")[0];
};
if(get_resource_href.indexOf("resource_name=") != -1) {
var has_resource_name =get_resource_href.split("resource_name=")[1];
var siderbar_index =has_resource_name;
} else if(get_resource_href.indexOf("report_name=") != -1) {
var has_resource_name =get_resource_href.split("report_name=")[1];
var siderbar_index =has_resource_name.split("_REP")[0];
};
if (!post_page_index ||$("#personalInfo").length <= 0) {
if (!post_url) {
window.location.href ="MODULE";
}
$.ajax({
type: "post",
url: post_url,
data: post_params,
success: function(res){
$(&#39;#pageContainer&#39;).html(res);
if(post_page_index) {
location.href= location.href.split("!")[0] + "!1";
} else {
location.href= location.href.split("!")[0];
};
},
error: function(res) {
window.location.href = "MODULE";
},
});
}
//for request page next&back
if (post_page_index) {
var previous_index =$(".navbar,.steps .navbar-innerul.row-fluid").find("li.active").find(".number").text();
var differ =post_page_index - previous_index;
lock_for_req_back_next =1;
if (differ > 0) {
for (var i = 0; i <differ; a="" bar="" differ="-differ;" else="" for="" i="0;" if="" li="" lock_for_req_back_next="0;" resource_name="$(this).attr("href").split("resource_name=")[1];" side="" siderbar_index="=" ul.page-sidebar-menuli="" ul.sub-menu="" var=""> span.arrow').addClass('open');
$(this).parents('.sub-menu').show();
});
$(this).parent('li').parents('li').addClass('active open');
return false;
} else {
$('.sub-menu').hide();
}
});
$("ul.page-sidebar-menuli").not(".open").find("ul").hide();
}
}
</differ;>
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

怎么使用Ajax实现循环

ajax加载超时提示怎样实现

The above is the detailed content of How to deal with data after Ajax lost refresh. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template