我有两页A.html和B.html。对于A,我将使用jQuery向服务器发出Ajax请求,将运行长任务并在成功回调函数中接收数据。但是我会在Ajax请求之后导航到B.我想对第B页上的显示做出回应。请帮我设计一个解决方案。谢谢
答案 0 :(得分:0)
您可以将用户重定向到B.html
并将数据作为网址参数发送。
在A.html
:
function callback(data) {
//run code to check to make sure the data is valid, and stuff like that
//then redirect the user to B.html with the data as a URL parameter
window.location.href = "/B.html?data=" + encodeCallbackData(data);
}
您需要实现encodeCallbackData()
功能。
如果响应类型是JSON,它可能如下所示:
function encodeCallbackData(data) {
return encodeURIComponent(JSON.stringify(data));
}
然后,在B.html
if (getParameterByName("data") !== null) {
var json = JSON.parse(decodeURIComponent(getParameterByName("data")));
// json is a variable containing the object resulting from the AJAX request.
//do something with json
//show the success message.
} else {
//the user was not redirected from `A.html`.
}
getParameterByName
可以在this堆栈溢出答案中找到。