如何使用AJAX将数据发布到服务器/数据库 [英] How to post data onto server/database using AJAX
本文介绍了如何使用AJAX将数据发布到服务器/数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的控制器将数据发布到数据库中: -
[HttpPost]
public ActionResult UpdateGLCode( string Glcode, string ReportId, string HeadName)
{
SqlConnection con = null ;
SqlCommand cmd = null ;
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ System.Data.SqlClient]。ToString());
con.Open();
cmd = new SqlCommand( AliUpdateGLCodeInRLMaster ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( @ Criteria1,SqlDbType.VarChar).Value = Glcode;
cmd.Parameters.Add( @ Criteria2,SqlDbType.VarChar).Value = ReportId;
cmd.Parameters.Add( @ Criteria3,SqlDbType.VarChar).Value = HEADNAME;
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction( Index, new {msg = 记录保存成功。 });
}
及以下是我的Ajax:
$(#btnRLMasterSave)。click(function(){
alert(HI);
$ .ajax({
url:/ RLMaster / UpdateGLCode,
data:{Glcode:$('#Glcode')。val(),ReportId:$('#ReportId')。val(),HeadName:$('#HeadName')。 val()},
类型:POST,
dataType:json,
成功:函数(数据){
alert(HI);
},
错误:function(){
alert(失败);
}
});
});
我非常肯定我不对。
但这就是我自己写的全部。
请更正或任何建议都将受到高度赞赏。
解决方案
(#btnRLMasterSave)。click(function(){
alert( HI);
.ajax的({
url:/ RLMaster / UpdateGLCode,
data:{Glcode:
('#Glcode')。val(),ReportId:
This is my Controller to post data into database:-
[HttpPost]
public ActionResult UpdateGLCode(string Glcode, string ReportId, string HeadName)
{
SqlConnection con = null;
SqlCommand cmd = null;
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["System.Data.SqlClient"].ToString());
con.Open();
cmd = new SqlCommand("AliUpdateGLCodeInRLMaster", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Criteria1", SqlDbType.VarChar).Value = Glcode;
cmd.Parameters.Add("@Criteria2", SqlDbType.VarChar).Value = ReportId;
cmd.Parameters.Add("@Criteria3", SqlDbType.VarChar).Value = HeadName;
cmd.ExecuteNonQuery();
con.Close();
return RedirectToAction("Index", new { msg = "Record Saved Successfully." });
}
and below is my Ajax:
$("#btnRLMasterSave").click(function () {
alert("HI");
$.ajax({
url: "/RLMaster/UpdateGLCode",
data: { Glcode: $('#Glcode').val(), ReportId: $('#ReportId').val(), HeadName: $('#HeadName').val() },
type: "POST",
dataType: "json",
success: function (data) {
alert("HI");
},
error: function () {
alert("Failed");
}
});
});
I am very sure that I am not correct.
But this is all I can write of my own.
Please correct or any suggestions would be highly appreciated.
解决方案
("#btnRLMasterSave").click(function () { alert("HI");
.ajax({ url: "/RLMaster/UpdateGLCode", data: { Glcode:
('#Glcode').val(), ReportId:
这篇关于如何使用AJAX将数据发布到服务器/数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文