如何将JavaScript成功数据返回到jQuery属性

时间:2018-11-24 06:53:59

标签: javascript

下面的函数工作正常,我正在从ajax get函数获取响应,数据以JSON格式以“ res.data.value”格式传入,现在我想将此数据传递给数组中完整日历的events属性格式。如果我将静态值传递给事件属性,则工作正常。请帮助我,因为我无法动态传递'res.data.value'值。

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white // This is optional
    );
  }
}

/ *  *函数使用AJAX,Deffered和Promise进行API调用  * /

<script>

    $(document).ready(function() {

    var completedInterviews = function() {
        var requestParams = [];
        requestParams = {
                "url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
                "requestType": "GET",
                "headers": {
                    "Accept": "application/json"
                }
        }

        var res = makeRequest(requestParams);
        res
        .done(function(res) { 
           return  [res.data.value];
        })
        .fail(function(err) {
            console.log(err);
        });

    };    

    $('#calendar').fullCalendar({
      header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay,listWeek'
      },
      defaultDate: '2018-03-12',
      editable: true,
      navLinks: true, 
      eventLimit: true, 
      events: completedInterviews(),  // I want [res.data.value] value here.
      loading: function(bool) {
        $('#loading').toggle(bool);
      }
    });

});

</script> 

1 个答案:

答案 0 :(得分:0)

在then语句中使用日历。

<script>

    $(document).ready(function() {

    var completedInterviews = function() {
        var requestParams = [];
        requestParams = {
                "url": dashboardServiceUrl + '/dashboard/widget/completedinterview',
                "requestType": "GET",
                "headers": {
                    "Accept": "application/json"
                }
        }

        res = makeRequest(requestParams);

        //RETURN RES
        return res;

    };

    //WRAP CALENDAR WITH THEN
    completedInterviews().then(function(data){
        $('#calendar').fullCalendar({
          header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay,listWeek'
          },
          defaultDate: '2018-03-12',
          editable: true,
          navLinks: true, 
          eventLimit: true, 
          events: data,  // ADD DATA.
      loading: function(bool) {
        $('#loading').toggle(bool);
      }
    });
    });



});

</script>