私は基本的に以下のリンクで説明されているものと同じ問題がありますが、解決策がはっきりと分かりません。ウィンドウ関数が実行を終了するまで待つように私のAjax成功関数を設定したいのですが、divを変更してください。代わりに、現在のページのdivを変更してリダイレクトします。 AJAX: on success redirect then modify new pageAjaxはリダイレクトが完了するまで待機します。
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);
}
});
});
まず、ウィンドウ関数が実行を完了すると、同じページには定義されていません。だから、Ajaxの成功のコードの残りの部分は利用できません。つまり、あなたは正しい方法で考えているわけではありません。解決策は、あなたが提供したリンクにあります。 –
コールバックでdivの変更を実装する – manashb
@JonathanLonowskiより安全な方法は何でしょうか? –