Ajax等到重定向完成执行.

Mar*_*her 1 javascript ajax jquery

我有与下面链接中描述的问题基本相同的问题,但我没有找到解决方案非常清楚.我希望我的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);
  }
});

});
Run Code Online (Sandbox Code Playgroud)

Pra*_*til 6

我会建议你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);
  }
});

});
Run Code Online (Sandbox Code Playgroud)

在同一页面上准备就绪:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

浏览器存储中的本地存储存储数据.您还可以从本地存储中删除数据.