在回调函数之外获取响应的值

时间:2019-06-13 08:47:41

标签: ajax callback response

我在第一个ajax加载中获取nextPage值,但是,我想保存该值,以便以后使用它来请求数据。而且,我做了一个回调函数,以便可以在ajax之外获取nextPage的值。

因此,我使用了.done(),并制作了一个回调函数,该函数返回ajax之外的值。但是,现在我想要的是在回调函数之外设置nextPage的值。

var nextToken = "";
        function functionCallBack(response) {
            if (response.status.status == true && response.body.nextPageToken !== 'undefined') {
                nextToken = response.body.nextPageToken;
                console.log(nextToken)
            }
        }
        $('#prev').hide();
        var offset = 0;
        function call_ajax(){
         return $.ajax({
                url: '/process/videoPodcast.php',
                type: 'POST',
                data: {
                offset: offset  
            },
         });
        }
        $.when(call_ajax()).done(function(response){
        if (typeof(response) != 'object') {
              response = $.parseJSON(response);
            }
        if(response.body.nextPageToken !== 'undefined'){
            nextToken = response.body.nextPageToken;
        } else {
            nextToken = 0
        }
        if (response.status.status == true) {
              var html_option = "<div class='top-with-controls-archive text-center'></div><div class='carousel-inner'><div class='carousel-item active'><div class='row py-2'>"
              $.each(response.body.items, function(key, value) {
                var dateTruncated = value.id;
                var myStr = value.snippet.title;
                var strArray = myStr.replace(/\s/gi, "-");
                html_option += "<div class='col-md-3 col-sm-6'><div class='body-archive'><a href='/podcasts/" + dateTruncated + "/"+strArray+"'><img src='" + value.snippet.thumbnails.medium.url + "' class='img-fluid'></a><h5 class='text-center'>" + myStr + "</h5></div></div>";
              });
              html_option += "</div>";
              $('#wrap').html(html_option);
            }
            functionCallBack(response);// when I do console.log(nextToken) here 
        });// when I do console.log(nextToken) here it does not return a value

是否可以在回调函数之外获取值?

0 个答案:

没有答案