在asp.net c#中使用jquery ajax向SQL数据库中插入数据
本文关键字:SQL 数据库 数据 插入 ajax net asp jquery | 更新日期: 2023-09-27 18:06:06
我使用三层架构,并尝试使用jquery ajax将数据存储到数据库中,但在成功函数
中没有得到响应这是我的代码
portalDAL.cs
public DataTable InsertFeedBack(String Name, String Email, string Category, string Message)
{
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@Name",Name),
new SqlParameter("@Email",Email),
new SqlParameter("@Category",Category),
new SqlParameter("@Message",Message)
};
return Helper.ExecuteParamerizedSelectCommand("insert into feedback(name,email,category,message) values(@Name,@Email,@Category,@Message)", CommandType.Text, parms);
}
portalBAL.cs
public DataTable InsertFeedBack(String Name, String Email, string Category, string Message)
{
return portalDAL.InsertFeedBack(Name, Email, Category, Message);
}
portal.asmx.cs
[WebMethod]
public String InsertFeedBack(String Name, String Email, string Category, string Message)
{
DataTable dt = detailsBAL.InsertFeedBack(Name, Email, Category, Message);
return JsonConvert.SerializeObject(dt);
}
My Jquery Function.
$(document).ready(function () {
$('#submit').click(function () {
var name = $('#name').val();
var email = $('#email').val();
var category = $('#cate').val();
var msg = $('#msg').val();
insertFeedback(name,email,category,msg);
});
function insertFeedback(name,email,cat,msg)
{
$.ajax({
type: "POST",
url: "portal.asmx/InsertFeedBack",
data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("hi");
var obj = data.d;
if (obj == 'true') {
$('#name').val('');
$('#email').val('');
$('#cate').val('');
$('#msg').val('');
$('#lblmsg1').html("Details Submitted Successfully");
window.location.reload();
}
},
error: function (result) {
alert("Error");
}
});
}
});
我得到错误警报消息控件没有进入成功功能,它没有显示浏览器上的任何错误
试试下面的代码
$('#submit').click(function () {
insertFeedback();
});
function insertFeedback()
{
var model = new Object();
model.name = $('#name').val();
model.email = $('#email').val();
model.category = $('#cate').val();
model.msg = $('#msg').val();
$.ajax({
type: "POST",
url: "portal.asmx/InsertFeedBack",
data: model,
dataType: "json",
success: function (data) {
alert("hi");
// your code
},
error: function (result) {
alert("Error");
}
}
创建一个具有四个属性的类
public class YourClass
{
public string name { get; set; }
public string email { get; set; }
public string category { get; set; }
public string message { get; set; }
}
将方法参数更改为类对象。你可以用一个对象从ajax调用中接收多个参数。
[WebMethod]
public String InsertFeedBack(YourClass model)
{
DataTable dt = detailsBAL.InsertFeedBack(model.name, model.email, model.category, model.message);
return JsonConvert.SerializeObject(dt);
}
尝试使用这个ajax代码格式。
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}",
url: "portal.asmx/InsertFeedBack",
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
}
});