locked
Delete Record RRS feed

  • Question

  • User-797751191 posted

    Hi

       I have below code and i want to know in function Delete(UsrName)

    what does JSON.stringify does. Is it necessary to use JSON.stringify. Any other method we can use.

    When i try like this - data: { "UsrName": UsrName }, then it gives error Invalid JSON primitive :UsrName

    $(document).ready(function () {
        $('#tblUser').dataTable();
        loadData();
        
    });
    
    function loadData() {
        $.ajax({
            url: "/Home/List",
            type: "GET",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                var html = '';
                $.each(result, function (key, item) {
                    html += '<tr>';
                    html += '<td>' + item.UserName + '</td>';
                    html += '<td>' + item.Password + '</td>';
                    html += '<td>' + item.FirstName + '</td>';
                    html += '<td>' + item.LastName + '</td>';
                    html += '<td><a href="#" onclick="return getbyName(\'' + item.UserName + '\')">Edit</a> | <a href="#" onclick="Delete(\'' + item.UserName + '\')">Delete</a></td>';
                    html += '</tr>';
                });
                $('.tbody').html(html);
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
    
    function Delete(UsrName) {
        var dataObject = JSON.stringify({
            'UsrName': UsrName,
        });
        var ans = confirm("Are you sure you want to delete this Record?");
        if (ans) {
            $.ajax({
                url: '/Home/DeleteUser',
                //data: JSON.stringify({ UsrName: UsrName }),
                //data: { UsrName: UsrName },
                data: dataObject,
                type: "POST",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    loadData();
                },
                error: function (errormessage) {
                    alert(errormessage.responseText);
                }
            });
        }
    }
    
    
    public JsonResult DeleteUser(string UsrName)
            {
                return Json(usrDB.DeleteUser(UsrName), JsonRequestBehavior.AllowGet);
            }

    Thanks

    Wednesday, October 30, 2019 5:39 AM

Answers

  • User665608656 posted

    Hi jsshivalik,

    According to the code you provided, when you pass parameters using ajax, you change contentType at this time.

    $. ajax contentType and dataType. contentType mainly sets the format you send to the server, and dataType sets the format you receive from the server.

    Get and post are the most common HTTP requests. In jQuery's ajax, contentType is the default value: application/x-www-form-urlencoded.

    The feature of this format is that name / value becomes a group, and each group uses & connection, while name and value use = connection.

    Here you only pass the one parameter, so you don't need to add contentType to change the type.

    The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

    You can change your code like this:

     function Delete(UsrName) {
              
                var ans = confirm("Are you sure you want to delete this Record?");
                if (ans) {
                    $.ajax({
                        url: '/Home/DeleteUser', 
                        data: { UsrName: UsrName },
                        type: "POST",
                       // contentType: "application/json;charset=UTF-8",
                        dataType: "json",
                        success: function (result) {
                            loadData();
                        },
                        error: function (errormessage) {
                            alert(errormessage.responseText);
                        }
                    });
                }
            }

    Here is the result you can refer :

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 30, 2019 7:20 AM