Ajax等待重定向以完成执行
Ajax wait till redirect to finish executing.
我遇到的问题与下面链接中描述的问题基本相同,但我觉得解决方案不是很清楚。我希望我的ajax成功函数等待窗口函数执行完毕,然后修改div。相反,它修改当前页面的div,然后重定向。AJAX:成功重定向后修改新页面
main.js
$("#form").submit( function(e) {
e.preventDefault();
var id = $('#searchbar').val(); // store the form's data.
$.ajax({
url: '/search',
type: 'POST',
data: {id:id},
dataType: 'text',
success: function(data) {
//Redirect to the page where we want to display the data
window.location.href = '/test';
data = JSON.parse(data);
console.log(data);
$("#count").text("we analyzed...");
$("#result1").text(data.county);
$("#totals").text("with a score of..");
$("#result2").text(data.totalSentiments);
},
error: function(jqXHR, textStatus, errorThrown){
console.log("error")
alert(textStatus, errorThrown);
}
});
});
我建议您使用Javascript本地存储。
main.js
$("#form").submit( function(e) {
e.preventDefault();
var id = $('#searchbar').val(); // store the form's data.
$.ajax({
url: '/search',
type: 'POST',
data: {id:id},
dataType: 'text',
success: function(data) {
//Redirect to the page where we want to display the data
window.location.href = '/test';
data = JSON.parse(data);
console.log(data);
// Store
localStorage.setItem("count", "we analyzed...");
localStorage.setItem("result1", data.county);
localStorage.setItem("totals", "with a score of..");
localStorage.setItem("result2", data.totalSentiments);
},
error: function(jqXHR, textStatus, errorThrown){
console.log("error")
alert(textStatus, errorThrown);
}
});
});
在同一页面上的就绪:
jQuery(document).ready(function(){
if (localStorage.count) {
$("#count").text(localStorage.count);
}
if (localStorage.result1) {
$("#result1").text(localStorage.result1);
}
if (localStorage.totals) {
$("#totals").text(localStorage.totals);
}
if (localStorage.result2) {
$("#result2").text(localStorage.result2);
}
});
本地存储将数据存储在浏览器存储中。您还可以从本地存储中删除数据。
设置location.href
的值将导致整页刷新。
因此,您的所有脚本都将被删除。
如果你真的想使用ajax调用重定向页面的结果,你应该把这个响应数据存储在某个地方,然后在你的新页面上重用它。
//save "data" in localSotorage
localStorage.myAjaxResponse = data; //if data is JSON then use: JSON.stringify(data) instead.
然后在"/test"页面上,创建一个脚本来检查localStorage上的值,然后显示它
data = JSON.parse(localStorage.myAjaxResponse);
console.log(data);
$("#count").text("we analyzed...");
$("#result1").text(data.county);
$("#totals").text("with a score of..");
$("#result2").text(data.totalSentiments);
尽管如此,还有其他更好的方法来实现你想要的。
您可以这样做:
关于ajax的成功:
data = JSON.parse(data);
console.log(data);
window.location.href = '/test?county='+data.county+'&sentiment='+totalSentiments;
然后在你的test
页面上写下javascript块:
var params={};
window.location.search
.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(str,key,value) {
params[key] = value;
}
);
if (params.length > 0) {
$("#count").text("we analyzed...");
$("#result1").text(params['county']);
$("#totals").text("with a score of..");
$("#result2").text(params['sentiments']);
}
相关文章:
- 等待回调函数执行
- 如何在seleniumwebdriver中执行javascript提示并等待接受输入
- Node.js-等待process.exit()在前面的代码完成后执行
- Javascript等待ajax成功后再执行代码
- 等待函数执行后再继续
- 必须等待执行 jQuery,直到加载服务
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- JavaScript setTimeout() 不会等待执行
- Ajax等待重定向以完成执行
- 动画在执行之前等待
- 等待JQuery执行,直到加载了整个页面,中断执行
- 使用jQuery并行执行多个promise后等待完成
- 如何等待ajax成功处理程序完成后再执行另一个
- jQuery-等待此元素,然后执行1次
- 等待promise解析后再执行代码
- 如何在函数执行前等待一秒钟
- 并行执行功能并等待其完成
- 如何在执行之前使 JavaScript 中的内联表达式等待
- 等待 AJAX,然后再继续执行单独的函数
- 我如何执行等待操作作为while条件或等效条件