我正在使用三层架构,并尝试使用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);
}
我的Jquery功能。
$(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");
}
});
}
});
我收到错误警告消息控制没有进入成功功能它在浏览器上没有显示任何错误
答案 0 :(得分:1)
尝试使用这种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);
}
});
答案 1 :(得分:1)
尝试使用此代码
$('#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);
}