C# 从JQuery在代码隐藏中调用方法
我正在尝试从Jquery在ASP.NET中执行一个方法C# 从JQuery在代码隐藏中调用方法,c#,asp.net,jquery,C#,Asp.net,Jquery,我正在尝试从Jquery在ASP.NET中执行一个方法 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "MyMessages.aspx?id=66&epslanguage=sv/test", data: "{}", dataType: "json", error: function(xhr
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "MyMessages.aspx?id=66&epslanguage=sv/test",
data: "{}",
dataType: "json",
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
},
success: function() {
alert("it works" );
}
})
代码隐藏:
[WebMethod]
protected void test()
{
test.Text = "works";
}
执行此操作时,我会收到错误消息redayState:4和状态200。我不明白这个问题。我对这一点很陌生。:)
数据:{},
应该只是数据:{},
或者如果不使用就删除它,…首先,您的方法应该是静态的和公共的
所以不是这个
[WebMethod]
protected void test()
{
test.Text = "works";
}
应该是
[WebMethod]
public static void test()
{
test.Text = "works";
}
这是第一次修复
第二次您无法访问test.text
所以,如果你想确保它的工作,你可以
这样写吧
$.ajax({
type: "POST",
contentType: "application/json",
url: "MyMessages.aspx/test",
data: "{}",
dataType: "json",
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
},
success: function() {
alert("it works" );
// and here use jQuery to set the control test
$("#<%=test.ClientID%>".text("Works");
}
})
和Ajax方法
$.ajax({
type: "POST",
contentType: "application/json",
url: "MyMessages.aspx/test",
data: "{}",
dataType: "json",
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
},
success: function() {
alert("it works" );
// and here use jQuery to set the control test
$("#<%=test.ClientID%>".text("Works");
}
})
我希望它能帮助你
地址+“}”欢迎访问SO,请访问可能的副本
$.ajax({
type: "POST",
contentType: "application/json",
url: "MyMessages.aspx/test",
data: "{}",
dataType: "json",
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
},
success: function() {
alert("it works" );
// and here use jQuery to set the control test
$("#<%=test.ClientID%>".text("Works");
}
})
'{"fname":"' + fname + '","lname":"' + lname + '","email":"' + email + '","address":"' +