Ajax功能不起作用

时间:2016-09-20 11:12:08

标签: c# jquery ajax

enter image description here Ajax函数不更新数据。数据加载在bootstrap的对话框中,我需要更改数据更改。我也检查浏览器的值是来自对话框但不是在数据库中发布..如何解决..提前谢谢

客户端:

$("#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();
    }
} 

2 个答案:

答案 0 :(得分:1)

我认为你应该发布你的数据而不用你的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

答案 1 :(得分:1)

首先,你有什么错误信息?

在测试Ajax和amp;之间的通信之前C#更新功能,你需要测试连接是否是功能。

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

如果更新表成功,请检查您的表是否已更新。 您需要调试Ajax数据请求。

感谢@Nsevens代码:

$("#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);
        }
    });
});

使用console.log进行调试:

  

console.log(&#34; userArray:&#34; + user);

它可能会有所帮助,当您遇到类似问题时,检查您发送的数据非常重要。