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

时间:2014-07-27 01:08:46

标签: javascript jquery html ajax html5

我希望在刷新页面时执行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;
});

1 个答案:

答案 0 :(得分:1)

从您的评论中可以看出,当您的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
相关问题