1

I am new to Ajax. Trying to fetch JSON data returned from Get webAPI from controllers but on button click nothing rendering on View.

This is how my view look like

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var ulEmployees = $('#ulEmployees');
            $('#btn').click(function () {
                    var id = $(this).attr(id);
                $.ajax({
                    url: '/api/employee', type: "GET", dataType: "json",
                    data: { id: id },
                    success: function (data) {
                        ulEmployees.empty();
                        $.each(data, function (index, val) {
                            var fullName = val.FirstName + ' ' + val.LastName;
                            ulEmployees.append('<li>' + fullName + '</li>')
                        });
                    }
                });
            });

            $('#btnClear').click(function () {
                ulEmployees.empty();
            });
        });
    </script>
</head>
<body>
    <input id="btn" type="button" value="Get All Employees" />
    <input id="btnClear" type="button" value="Clear" />
    <ul id="ulEmployees"></ul>
</body>
</html>

This is the JSON data returned by webapienter image description here

Can anyone help me what went wrong here? Thanks in advance.

1
  • 2
    There is typo near val.FirstName + ' ' + val.LastName should be val.firstName + ' ' + val.lastName; look closely for upper case and lower case in your json . Commented May 29, 2020 at 7:23

1 Answer 1

1

Below should work:

    $(document).ready(function () {
    var ulEmployees = $('#ulEmployees');
    $('#btn').click(function () {
        var id = $(this).attr('id');
        fetch('/api/employee?id=' + id)
        .then((resp) => resp.json())
        .then(function(data) {
        ulEmployees.empty();
            $.each(data, function (index, val) {
                var fullName = val.firstName + ' ' + val.lastName;
                ulEmployees.append('<li>' + fullName + '</li>');
            });
        })
        .catch(function(error) {
            console.log(error);
        });

    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this but still not getting the result.
I am now getting following error Uncaught TypeError: Cannot read property 'nodeType' of undefined at init.attr (jquery.min.js:5) at Function.access (jquery.min.js:4) at init.attr (jquery.min.js:5) at HTMLInputElement.<anonymous> (emp:10) at HTMLInputElement.dispatch (jquery.min.js:5) at HTMLInputElement.v.handle (jquery.min.js:5)
this error is near attr because id should be in quote i.e : $(this).attr('id')
@Swati this solve my error and after making the val.firstName + ' ' + val.lastName i.e. lower casing attribute, I am able to get the result. Thanks a tone. Saved my day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.