Ad

Using An Object I Never Defined In JQuery

- 1 answer

I'm learning about building MVC5 web pages using JQuery and Ajax, in one of my exercises I created this function:

<script language="javascript">
    $.get("GetCustomersByJson", null, BindData);

    function BindData(customers) {
        var tbl = $("#customerTable");
        for (k = 0; k < customers.length; k++) {
            var newRow = "<tr>" +
                "<td>" + customers[k].FirstName + "</td>" +
                "<td>" + customers[k].LastName + "</td>" +
                "<td>" + customers[k].CustomerNumber + "</td>" +
                "</tr>";
            tbl.append(newRow);
        }
    }
</script>

Its basically loading some "Customer" objects using Json and adds them to the table after loading them.

My problem is with the BindData function, it gets the customers parameter but how does it connect with the $.get("GetCustomersByJson", null, BindData); line?

I understand it's probably basic JS knowledge but i still can't find the answer to it.

Ad

Answer

Your giving the function to the $.get() method as a success callback. When the ajax returns it executes that method, providing it with the response data.

Ad
source: stackoverflow.com
Ad