如果页面被刷新,如何运行ajax脚本

How to run ajax script if the page is refreshed

本文关键字:运行 脚本 ajax 何运行 刷新 如果      更新时间:2023-09-26

我希望ajax脚本在页面刷新时执行。

我的问题是它不工作- ajax脚本没有运行。

脚本:

$(window).bind("load", function() {
    var data = {};
        data.emailCodeResult = $('#emailCodeResult').val();
    $.ajax({
        type: "POST",
        url: "Oppa/view/viewOther.php",
        data: data,
        cache: false,
        dataType:"JSON",
        success: function (result) {

            $("#prof_talent").val(result.oth_hobbies);
            $("#prof_recognition").val(result.oth_recognition);
            $("#prof_organization").val(result.oth_organization);
            $("#prof_workExperience").val(result.oth_workExperience);
            $("#prof_trainingPrograms").val(result.oth_trainingPrograms);
            $(".show-page[data-page=Profile_Other_info]").trigger("click"); 
        }
     });
            return false;
});

从您的评论中听起来,当您的AJAX调用触发时,$('#emailCodeResult').val()仍然为空。既然你使用setinterval不断地重新填充它,为什么不在AJAX调用之前放一个do/while循环,以确保data.emailCodeResult得到内容?

//helper function (see http://www.sitepoint.com/delay-sleep-pause-wait/)
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}
do{
    sleep(2000);
}
while($('#emailCodeResult').val() == 'undefined');
//AJAX call here