本文介绍了Ajax功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ajax函数不会更新数据.引导程序对话框中的数据加载,我需要通过更改来更新数据.我还要在浏览器中检查该值是否来自对话框,而不是发布到数据库中..如何解决..在此先感谢

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 but not posting in database..How to solve..Thanks in advance

客户端:

$("#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:JSON.stringify( '{objEmployee: ' + JSON.stringify(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);
        }
    });
});

服务器端:

[WebMethod]
public static void UpdateData(DayBooks objEmployee, string eid) //Update data in database
{
    using (var con = new SqlConnection(strConnection))
    {
        var query = "update DayBooks set DayDesc='" + objEmployee.DayDesc + "',VoucherNo='" + objEmployee.VoucherNo +
                    "',VoucherNo1='" + objEmployee.VoucherNo1 + "'where DayCode='" + eid + "'";
        con.Open();
        var cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

推荐答案

我认为您应该在不对JSON进行字符串化的情况下发布数据:

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);
        }
    });
});

另一方面,请注意:不要在数据库语句中进行SQL注入...请改用参数以避免可能的安全漏洞.更多信息@ https://stackoverflow.com/a/6548006/2805121

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

这篇关于Ajax功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:13