datepicker - 来自csv-file的unavailableDates(datepicker - unavailableDates from csv-file)

我正试图从csv文件中获取我的datepicker-calendar的不可用日期。

csv文件结构如下所示:

26.08.2015 05.08.2015

我从csv文件中获取数据的请求如下所示:

var unavailableDates = $.ajax({ type: "GET", url: "http://www.website.de/dates.csv", dataType: "text", success: function(data) { console.log(data); } }); alert (unavailableDates);

consolge-log输出:

26.08.2015 05.08.2015

但警报弹出窗口说:

[object Object]

那么如何才能正确地从csv中获取数据,我需要这个结构中的这些数据:

var unavailableDates = ["19-8-2015","14-8-2015"];

任何帮助深表感谢!

编辑:

我使用连字符而不是点来管理以正确方式创建csv文件,因此csv-structure现在看起来像这样:

26-08-2015 05-08-2015

但我还是得到了

[object Object]

从警报弹出窗口。

I'm trying to get unavailable dates for my datepicker-calendar off a csv-file.

The csv-file structure looks like this:

26.08.2015 05.08.2015

My call for getting the data off the csv-file looks like this:

var unavailableDates = $.ajax({ type: "GET", url: "http://www.website.de/dates.csv", dataType: "text", success: function(data) { console.log(data); } }); alert (unavailableDates);

The consolge-log outputs this:

26.08.2015 05.08.2015

But the alert-popup says:

[object Object]

So how can I get the data off the csv-correctly, I need these data in this structure:

var unavailableDates = ["19-8-2015","14-8-2015"];

Any help is much appreciated!

EDIT:

I managed the creating of the csv-file in the correct way with a hyphen instead of a dot, so the csv-structure now looks like this:

26-08-2015 05-08-2015

But I'm still getting

[object Object]

from the alert popup.

最满意答案

您可以使用库https://github.com/evanplaice/jquery-csv/将cvs转换为数组。 由于您使用的是Ajax回调函数,因此只能在成功函数内获取结果。 所以你可以做的是,创建另一个函数,将csv数组作为参数并在成功回调中调用它。 检查以下代码:

var unavailableDates = $.ajax({ type: "GET", url: "http://www.website.de/dates.csv", dataType: "text", success: function(data) { console.log(data); var csvArray= $.csv.toArray(data); processCsvArray(csvArray); } }); function processCsvArray(csvData){ //do your work }

You can use library https://github.com/evanplaice/jquery-csv/ for cvs to array. since you are using Ajax callback function, you can only get the result inside success function. so what you can do is, create another function which take the csv array as a parameter and call it inside success callback. check below code:

var unavailableDates = $.ajax({ type: "GET", url: "http://www.website.de/dates.csv", dataType: "text", success: function(data) { console.log(data); var csvArray= $.csv.toArray(data); processCsvArray(csvArray); } }); function processCsvArray(csvData){ //do your work }

更多推荐