在日历函数中使用promises响应(需要来自ajax调用的数据)

时间:2017-03-10 07:51:55

标签: javascript php ajax promise

Ajax呼吁承诺:

<h2>

拒绝承诺

var promise = new Promise(function(resolve, reject) {
    jQuery.ajax({
            url:'<?php echo site_url();?>/MyController/ajax_cal',
            type:'POST',
            dataType: "JSON",
            success:function(results) {
                resolve(results); 
                //reject(Error(request.statusText)); // status is not 200 O K, so reject
            }
        });
  });

Calander javascript代码:

promise.then(function(data) {
      alert(data); // this is working fine BUT i want to use this in calendar function 

 }, function(error) {
    console.log('Promise rejected.');
    console.log(error.message);
  }); 

我想在&#34;日历events_source字段&#34;中获取动态数据(json格式)。 。我使用了ajax调用,但我知道变量在ajax调用之外是不可访问的。我比使用promises成功提醒ajax调用之外的变量但是我仍然无法在&#34; calendar events_source字段中访问它#34; 。我不知道什么是错的以及我如何使用它。

1 个答案:

答案 0 :(得分:0)

解决这个问题的一种方法是在拥有数据后创建日历元素(然后阻止)。我可能会这样做,以便我构建UI然后触发提取,然后在then()函数中我将使用收到的事件调用 onAfterEventsLoad ,因为它似乎构建了日历。