Ajax function not working

前端 未结 2 1479
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 14:01

Ajax function not updating the data. Data load in dialogbox of bootstrap and i need to update the data with changes.i also check in browser the value is come from dialog box

相关标签:
2条回答
  • 2020-12-22 14:31

    First of all, What error message did you have?

    Before testing communication between Ajax & C# update function, you need to test if connection is functionnal.

    public static void UpdateData(DayBooks objEmployee, string eid) //Update data in database      
    {
        using (var con = new SqlConnection(strConnection))
        {
            var query = "UPDATE DayBooks set DayDesc=12345,VoucherNo=54321,VoucherNo1=1 where DayCode=12";
            con.Open();
            var cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    } 
    

    And check if your table has updated,if update table success. you need to debug Ajax data request.

    thanks to @Nsevens code :

    $("#UpdateTbl").click(function () {
        var id = $(this).attr("edit-id");        <- what's value of id
        var user = {};
        debugger;
        user.DayDesc = $("#DaybookDesc1").val(); <- what's value of user.DayDesc
        user.VoucherNo = $("#Prifix1").val();    <- what's value of user.VoucherNo
        user.VoucherNo1 = $("#Surffix1").val();  <- what's value of user.VoucherNo1
    
    
        $.ajax({
            type: "Post",
            contentType: "application/json; charset=utf-8",
            url: "DaybookMast.aspx/UpdateData",
            data: {"objEmployee": user, "eid": id}, <- what's value of data 
            dataType: "json",
            success: function (data) {
                if (confirm("Are you want to change !") == true) {
                    alert("Updated successfully");
                } else {
                    alert("canceled changes");
                }
            },
            error: function (data) {
                alert("Error while Updating data of :" + id);
            }
        });
    });
    

    use console.log for debugging:

    console.log("userArray : " + user);

    it's can be helpful, when you have a problem like that, it's important to check what data do you send.

    0 讨论(0)
  • 2020-12-22 14:41

    I think you should post your data without stringifying your JSON:

    $("#UpdateTbl").click(function () {
        var id = $(this).attr("edit-id");
        var user = {};
        debugger;
        user.DayDesc = $("#DaybookDesc1").val();
        user.VoucherNo = $("#Prifix1").val();
        user.VoucherNo1 = $("#Surffix1").val();
    
    
        $.ajax({
            type: "Post",
            contentType: "application/json; charset=utf-8",
            url: "DaybookMast.aspx/UpdateData",
            data: {"objEmployee": user, "eid": id},
            dataType: "json",
            success: function (data) {
                if (confirm("Are you want to change !") == true) {
                    alert("Updated successfully");
                } else {
                    alert("canceled changes");
                }
            },
            error: function (data) {
                alert("Error while Updating data of :" + id);
            }
        });
    });
    

    On a side note: don't do SQL injection in your DB statements... Use parameters instead to avoid possible security breaches. More info @ https://stackoverflow.com/a/6548006/2805121

    0 讨论(0)
提交回复
热议问题