C# 在asp.net c中使用jquery ajax将数据插入sql数据库#
我使用三层体系结构,并尝试使用jqueryajax将数据存储到数据库中,但在success函数中没有得到响应 这是我的密码 portalDAL.csC# 在asp.net c中使用jquery ajax将数据插入sql数据库#,c#,jquery,asp.net,ajax,C#,Jquery,Asp.net,Ajax,我使用三层体系结构,并尝试使用jqueryajax将数据存储到数据库中,但在success函数中没有得到响应 这是我的密码 portalDAL.cs public DataTable InsertFeedBack(String Name, String Email, string Category, string Message) { SqlParameter[] parms = new SqlParameter[]{ new SqlParameter("@Name",Name)
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");
}
});
}
});
我收到错误警报消息控件未进入成功功能它在浏览器上未显示任何错误尝试使用此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);
}
});
尝试使用这种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);
}
});
请尝试使用此代码
$('#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);
}
请尝试使用此代码
$('#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);
}
您是否在firebug控制台中检查了来自服务器的错误?如果是,您可以发布错误消息或错误文本的屏幕截图吗?它在控制台上没有显示任何错误。在收到错误消息后,警报将能够在控制台上看到此错误“未捕获异常无法转换为字符串”存在一些服务器端错误。在web方法上放置一个调试点,并检查它从何处获得此错误。您的意思是不能调用服务器端方法吗?所以,若您的web方法中有一个断点,那个么通过ajax调用它,断点并没有命中?您是否在firebug控制台中检查了服务器出现了什么错误?如果是,您可以发布错误消息或错误文本的屏幕截图吗?它在控制台上没有显示任何错误。在收到错误消息后,警报将能够在控制台上看到此错误“未捕获异常无法转换为字符串”存在一些服务器端错误。在web方法上放置一个调试点,并检查它从何处获得此错误。您的意思是不能调用服务器端方法吗?所以,若您的web方法中有一个断点,那个么通过ajax调用它,断点并没有命中吗?