Adding JSON results to JavaScript list

时间:2015-06-15 15:08:05

标签: javascript jquery arrays json

I have created a script to call a page, which returns a list of date strings via JSON. How do I add these dates to an array that I've created?

var bookedDates= [];
$(document).ready(function () {
    $.ajax({
        url: "/Ajax/getBooked",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert("success");
            //Add results to list here?
        }
    });
});

I'm new to JavaScript.

1 个答案:

答案 0 :(得分:3)

You can use Array.prototype.concat()

The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.

var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])

Code

bookedDates = bookedDates.concat(response)

Or, Use Array.prototype.push()

The push() method adds one or more elements to the end of an array and returns the new length of the array

Code

Array.prototype.push.apply(bookedDates, response);