简体   繁体   English

在Javascript函数中使用AJAX响应

[英]Use AJAX Response in Javascript function

Essentially, I have a list of clients and a calendar beside this. 本质上,我旁边有一个客户列表和一个日历。 The idea is that when a client is clicked then it find the availability and then return this information through a calendar plugin to show which days are available. 这个想法是,当单击客户时,它会找到可用性,然后通过日历插件返回此信息以显示哪些日期可用。 Below is my code inside the 'click' function: 以下是我在“点击”功能内的代码:

$.ajax({
    type: "GET",
    url: "inc/available_dates_search.php",
    data: string,
    cache: false,
    success: function(response){

        var result = eval(response);

        $('#date_calendar').glDatePicker({
            selectableDates: result
        });
    }
});

The property 'selectableDates' needs to be in the format [{ date: new Date(2013, 8, 15) }] when I perform alert(result) it gives me the correct output . 当我执行alert(result)[{ date: new Date(2013, 8, 15) }]属性'selectableDates'的格式应为[{ date: new Date(2013, 8, 15) }]它可以为我提供正确的输出 This leads me to believe that the problem is with passing the value to the datepicker. 这使我相信问题在于将值传递给datepicker。 Is it not possible to pass this response to the datepicker and if it is how can it be achieved? 无法将此响应传递给datepicker,如果可以实现,该方法如何?

I am using the glDatePicker, which can be found here: http://glad.github.io/glDatePicker/ 我正在使用glDatePicker,可以在这里找到它: http ://glad.github.io/glDatePicker/

Many thanks in advance. 提前谢谢了。

Shot in the dark since I never used glDatePicker 因为我从未使用过glDatePicker,所以在黑暗中拍摄

The problem is probably the fact the calendar already exists and it will not update. 问题可能是日历已经存在并且不会更新。 You will need to destroy it and re-initialize it. 您将需要销毁它并重新初始化它。 Or if the API has a way to update the dates, call that method. 或者,如果API有更新日期的方法,请调用该方法。

If it is written correctly and has a destroy method, something like this should work. 如果正确编写并具有destroy方法,则应执行类似操作。

$('#date_calendar').glDatePicker("destroy").glDatePicker({
     selectableDates: result
});

Try to use callback, response evaluation might take too long. 尝试使用回调,响应评估可能会花费太长时间。

$.ajax({
 type: "GET",
 url: "inc/available_dates_search.php",
 data: string,
 cache: false,
 success: function(response){
   (function(res, callback){
       // ... your code to process "res"
       result = xyz;
       callback(result);
   })(response, function(data){
       $('#date_calendar').glDatePicker({
          selectableDates: data
       });
   });

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM