Tuesday, 27 November 2018

MVC - JSON - fix errror where no .success nor .error are called


watch this example on YouTube



in my case ensure    users.url is there


to fix it replace

        function LoadUsers() {
            var users = {};
            users = '@Url.Action("GetUserList", "Home")';
            users.type = "POST";
            users.dataType = "json";
            users.contentType = "application/json";
            users.success = function (Users) {
                alert('i am here');
                if (Users.length > 0) {
                    $('#UserTable').append('<table>');
                    $('#UserTable').append('<tr><th>First Name</th><th>Last Name</th><th></th><th></th></tr>');
                    $.each(Users, function (i, User) {
                        $('#UserTable').append('<tr><td class="FirstName">' + User.UserFirstName +
                            '</td><td class="LastName">' + User.UserLastName +
                            '</td><td> <input type="hidden" id="hid" value="' + User.UserID +
                            '" /> <input type="submit" value="EDIT" name="btn" id="btnEdit" onClick="EditUser(this);" </td><td><a href="#" id="btnDelete" onclick="DeleteUser(this);">DELETE</a></td></tr>');
                    });
                    $('#UserTable').append('</table>');
                }
            },
            users.error = function (xhr, status, error) {
     
                var el = document.createElement('html');
            }
            $.ajax(users);
        }

with

        function LoadUsers() {
            var users = {};
            users.url = '@Url.Action("GetUserList", "Home")';
            users.type = "POST";
            users.dataType = "json";
            users.contentType = "application/json";
            users.success = function (Users) {
                alert('i am here');
                if (Users.length > 0) {
                    $('#UserTable').append('<table>');
                    $('#UserTable').append('<tr><th>First Name</th><th>Last Name</th><th></th><th></th></tr>');
                    $.each(Users, function (i, User) {
                        $('#UserTable').append('<tr><td class="FirstName">' + User.UserFirstName +
                            '</td><td class="LastName">' + User.UserLastName +
                            '</td><td> <input type="hidden" id="hid" value="' + User.UserID +
                            '" /> <input type="submit" value="EDIT" name="btn" id="btnEdit" onClick="EditUser(this);" </td><td><a href="#" id="btnDelete" onclick="DeleteUser(this);">DELETE</a></td></tr>');
                    });
                    $('#UserTable').append('</table>');
                }
            },
            users.error = function (xhr, status, error) {
     
                var el = document.createElement('html');
            }
            $.ajax(users);
        }

No comments:

Post a Comment