初始化ajax请求的最佳实践

时间:2014-04-23 08:47:05

标签: jquery ajax init

我有一个包含在函数中的ajax调用,以便进行

的轮询
function doPoll(){
        alert('GO POLL')
        setTimeout(function(){
            $.ajax({
                'method':'POST',
                'url':'/ajax/request_news',
                'beforeSend':function(){
                    alert('Before send')
                    date = new Date()
                    var timestamp_last_check = Math.floor(date.getTime()/1000)
                },
                'data':{'last_check':timestamp_last_check}, 
                'success':function(ret){
                    ret = $.parseJSON(ret)
                },
                'complete':function(){
                    doPoll()
                }

            })
       },5000)
    };

此请求不起作用,因为它表示var timestamp_last_check未初始化。我将它放在beforeSend中会起作用,但似乎在beforeSend调用之前检索数据上下文。

我无法在AJAX调用之外进行时间戳初始化,因为setTimeout会导致时间戳初始化和AJAX调用之间延迟5秒。

我看到它有$.ajaxSetup功能,但文档建议不要使用它。

初始化此timestamp_last_check var的最佳做法是什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

尝试

setTimeout(function () {
     var timestamp_last_check = 0; //declare here to make it available to data:
     $.ajax({
         'method': 'POST',
             'url': '/ajax/request_news',
             'beforeSend': function () {
             alert('Before send')
             date = new Date()
             timestamp_last_check = Math.floor(date.getTime() / 1000); //set Value here
         },

<强>问题

var timestamp_last_check仅限于beforeSend:,因此无法在该区域外访问。

'beforeSend': function () {
                 alert('Before send')
                 date = new Date()
                 var timestamp_last_check = Math.floor(date.getTime() / 1000)
             },

阅读What is the scope of variables in JavaScript?

<小时/> 在OP的评论

之后更新
function doPoll() {
    alert('GO POLL');
    setTimeout(function () {
    var date = new Date();
    var timestamp_last_check = Math.floor(date.getTime() / 1000);//set value here
        $.ajax({
            'method': 'POST',
                'url': '/ajax/request_news',
                'data': {
                'last_check': timestamp_last_check
            },
                'success': function (ret) {
                ret = $.parseJSON(ret)
            },
                'complete': function () {
                doPoll()
            }
        })
    }, 5000)
};

答案 1 :(得分:1)

function doPoll() {
    alert('GO POLL')
    var timestamp_last_check;
    setTimeout(function () {
        $.ajax({
            'method': 'POST',
            'url': '/ajax/request_news',
            'beforeSend': function (xhr, settings) {
                alert('Before send')
                date = new Date()
                timestamp_last_check = Math.floor(date.getTime() / 1000);
                settings.data.last_check = timestamp_last_check;
            },
            'success': function (ret) {
                ret = $.parseJSON(ret)
            },
            'complete': function () {
                doPoll()
            }

        })
    }, 5000)
};

请注意,我已在doPoll方法的范围内声明了timestamp_last_check。这使得它关闭了beforeSend。 beforeSend接受两个论点。第二个参数是设置对象,应该用于更改设置或在请求中添加任何数据。

相关问题