javascript - Ajax wait till redirect to finish executing. -
i have same problem 1 described in link below, dont find solution clear. want ajax success function wait until window function finished executing, modify divs. instead, modifies divs of current page, redirects. ajax: on success redirect modify new page
main.js $("#form").submit( function(e) { e.preventdefault(); var id = $('#searchbar').val(); // store form's data. $.ajax({ url: '/search', type: 'post', data: {id:id}, datatype: 'text', success: function(data) { //redirect page want display data window.location.href = '/test'; data = json.parse(data); console.log(data); $("#count").text("we analyzed..."); $("#result1").text(data.county); $("#totals").text("with score of.."); $("#result2").text(data.totalsentiments); }, error: function(jqxhr, textstatus, errorthrown){ console.log("error") alert(textstatus, errorthrown); } }); });
i suggest javascript local storage .
main.js
$("#form").submit( function(e) { e.preventdefault(); var id = $('#searchbar').val(); // store form's data. $.ajax({ url: '/search', type: 'post', data: {id:id}, datatype: 'text', success: function(data) { //redirect page want display 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 score of.."); localstorage.setitem("result2", data.totalsentiments); }, error: function(jqxhr, textstatus, errorthrown){ console.log("error") alert(textstatus, errorthrown); } }); });
on ready on same page:
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); } });
local storage store data in browser storage. can remove data local storage.
Comments
Post a Comment