C# WebMethod没有';不要在调试时被调用
我正在尝试使用C# WebMethod没有';不要在调试时被调用,c#,jquery,asp.net,webforms,C#,Jquery,Asp.net,Webforms,我正在尝试使用jQuery插入数据,之前已经这样做了。但就目前而言,我是在很长一段时间后才这样做的。现在的问题是,我正在尝试使用浏览器进行调试,可以看到Ajax调用实际上没有被调用。下面是我的场景:我有一个表数据,每一行都与一个按钮关联。如果单击该按钮,关联的行数据将插入数据库表中(但这里我在调试器中放置断点,以检查是否调用了web方法)。这就是我尝试过的: <title>Tutorial - Sample App</title> <script src="http
jQuery
插入数据,之前已经这样做了。但就目前而言,我是在很长一段时间后才这样做的。现在的问题是,我正在尝试使用浏览器进行调试,可以看到Ajax调用实际上没有被调用。下面是我的场景:我有一个表数据,每一行都与一个按钮关联。如果单击该按钮,关联的行数据将插入数据库表中(但这里我在调试器中放置断点,以检查是否调用了web方法)。这就是我尝试过的:
<title>Tutorial - Sample App</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblPersons" runat="server"></asp:Label>
</div>
</form>
<script>
$(".use-address").click(function () {
var $row = $(this).closest("tr"); //Get the row
var $text = $row.find(".val").text(); //Get the text or value
alert($text);
debugger;
var persons = new Array();
var person = {};
person.name = $row.find(".val").text();
persons.push(person);
//The Ajax call here
$.ajax({
type: "POST",
url: "/SampleWebForm.aspx/InsertPersons", //This isn't called actually and keeping the code in the same page - SampleWebForm.aspx
data: JSON.stringify(persons),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data + " record(s) inserted.");
}
});
});
</script>
教程-示例应用程序
$(“.use address”)。单击(函数(){
var$row=$(this).closest(“tr”);//获取行
var$text=$row.find(“.val”).text();//获取文本或值
警报(文本);
调试器;
var persons=新数组();
var person={};
person.name=$row.find(“.val”).text();
推(人);
//这里的Ajax调用
$.ajax({
类型:“POST”,
url:“/SampleWebForm.aspx/InsertPersons”,//实际上不调用它,并将代码保留在同一页面中-SampleWebForm.aspx
数据:JSON.stringify(个人),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据+“插入记录”);
}
});
});
然后这里的C#部分:
protected void Page_Load(object sender, EventArgs e)
{
lblPersons.Text = Concatenate();
}
public class Person
{
public string name { get; set; }
public string age { get; set; }
}
public List<Person> GetPersons()
{
List<Person> lst = new List<Person>
{
new Person { name = "John", age = "20" },
new Person { name = "Jack", age = "30" }
};
return lst;
}
public string Concatenate()
{
string data = "";
data += "<table id = 'tblPersons'" +
"<thead>" +
"<tr class='ui-widget-header'>" +
"<th>Name</th>" +
"<th>Age</th>" +
"</tr>" +
"</thead>" +
"<tbody>" +
"";
foreach (var item in GetPersons())
{
data += "<tr><td class='val'><span>" + item.name + "</span></td>" +
"<td>" + item.age + "</td><br/>" +
"<td><button type = 'button' id = 'btnSave'>Click Here</button><td><tr>" +
"</td>";
}
data += "" +
"</tbody>" +
"</table>";
return data;
}
[WebMethod]
public string InsertPersons(Person persons) //This is the method that's supposed to be hit while debugging but doesn't
{
string name = "";
name = persons.name;
return name;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
lblPersons.Text=Concatenate();
}
公共阶层人士
{
公共字符串名称{get;set;}
公共字符串年龄{get;set;}
}
公众人士名单
{
列表lst=新列表
{
新人{name=“John”,age=“20”},
新人{name=“Jack”,age=“30”}
};
返回lst;
}
公共字符串连接()
{
字符串数据=”;
data+=“您需要将函数声明为“Static”,否则它将无法工作
public static string InsertPersons(Person persons)
您需要将函数声明为“Static”,否则它将无法工作
public static string InsertPersons(Person persons)
确保您的代码达到可以发出ajax请求的程度
脚本管理器可能会有所帮助,其中启用了页面方法
可能会有所帮助。确保您的代码达到可以发出ajax请求的程度
脚本管理器可能会有所帮助,其中启用了页面方法
可能会有所帮助。这不是Web服务(.asmx),而是.aspx中公开的页面方法
使用注释[PageMethod]而不是webmethod注释,该注释不是webservice(.asmx),而是.aspx中的公开页面方法
使用注释[PageMethod]而不是webmethod注释我能够让它工作,我的错误是,我试图传递一个列表
,尽管C#方法有一个要传递的对象。而不是对象列表。因此我更改了以下内容:
debugger;
var persons = new Array();
var person = {};
person.name = $row.find(".val").text();
persons.push(person);
致:
刚刚删除了push()
方法和persons数组。感谢大家提出的宝贵建议。我能够让它工作,我的错误是,我试图传递一个列表,尽管C#方法有一个要传递的对象。不是对象列表。因此我更改了以下内容:
debugger;
var persons = new Array();
var person = {};
person.name = $row.find(".val").text();
persons.push(person);
致:
刚刚删除了push()
方法和persons数组。感谢大家提出宝贵建议。发出警报($text)单击按钮时浏览器中的值是否正确?是的,它是@Hitesh Gaur。问题是该服务在visual studio的调试模式下未被调用-似乎遗漏了一些内容。如果有帮助,您能否检查浏览器中开发人员工具的“网络”选项卡中的输出?并查看您收到的特定aj的响应ax请求。难道WebMethod
不需要static
方法吗?不,ajax实际上不会在network选项卡@Hitesh Gaur中被调用和检查。会发出警报($text)单击按钮时浏览器中的值是否正确?是的,它是@Hitesh Gaur。问题是该服务在visual studio的调试模式下未被调用-似乎遗漏了一些内容。如果有帮助,您能否检查浏览器中开发人员工具的“网络”选项卡中的输出?并查看您收到的特定aj的响应ax请求。难道WebMethod
不需要static
方法吗?不,ajax实际上没有在网络选项卡@Hitesh Gaur中被调用和检查。已经尝试过@Kevbo。不起作用,我的意思是该方法没有被调用。已经尝试过@Kevbo。不起作用,我的意思是该方法没有被调用。