我必须使用ajax调用来调用服务器端方法更新(输入参数)。当我运行代码时,我的ajax没有加载。我想将该数据更新到Jquery Data table.please给我建议
$(文件).on('点击','#btnsave',function(){
//have to get the emp_id
var id = $('#hdnid').val();
update(id);
});
功能更新(id){
var name = $('#txtempname').val();
var sal = $('#txtsal').val();
var Dept_Id = $('#ddllist').val();
$.ajax({
//var Data = "{ empname:" + name + "}";
url: 'jdatatable.aspx/update',//my .aspx page name
type: "POST",
data: '{ id: ' + id + ',empname: "' + name + '", sal: ' +sal + ', Dept_Id: ' + Dept_Id + ' }',
contentType: "application/json; charset=utf-8",
dataType: "json",
"success": function (data) {
alert("successfully done");
table.ajax.reload();
$('#txtempname').val('');
$('#txtsal').val('');
$('#ddllist').val('');
},
"error": function (data, xhr, status) {
alert(status);
}
});
jdatatable.aspx.cs
protected void Page_Load(object sender,EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
//的MyMethod
[System.Web.Services.WebMethod]
[ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json,XmlSerializeString = false)]
public void update(int id, string empname, int sal, int Dept_Id)
{
string s = ConfigurationManager.ConnectionStrings["dbconn"].ToString();
List li = new List();
SqlConnection con = new SqlConnection(s);
con.Open();
SqlCommand cmd = new SqlCommand("sp_update", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@Emp_Name", empname));
cmd.Parameters.Add(new SqlParameter("@Sal", sal));
cmd.Parameters.Add(new SqlParameter("@Dept_Id", Dept_Id));
cmd.ExecuteNonQuery();
con.Close();
}