Pra*_*hav 3 asp.net jquery webmethod
我在Asp.net中使用X-Editable插件.
我试过这个:使用.Net和C#Webmethods
但它给了我错误.它没有像应该的那样调用WebMethod.
如何解决这个问题呢?
请帮忙.
使用Javascript:
$('#username').editable({
url: function (params) {
return $.ajax({
url: 'Default.aspx/TestMethod',
data: JSON.stringify(params),
dataType: 'json',
async: true,
cache: false,
timeout: 10000,
success: function (response) {
alert("Success");
},
error: function () {
alert("Error in Ajax");
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<a href="#" id="username" class="myeditable">superuser</a>
Run Code Online (Sandbox Code Playgroud)
Default.aspx中的WebMethod:
[System.Web.Services.WebMethod]
public static String TestMethod(String params)
{
//access params here
}
Run Code Online (Sandbox Code Playgroud)
如果要调用页面方法,首先需要发出POST类型的请求(同时设置内容类型不会造成任何伤害):
$('#username').editable({
url: function (params) {
return $.ajax({
type: 'POST',
url: 'Default.aspx/TestMethod',
data: JSON.stringify(params),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: true,
cache: false,
timeout: 10000,
success: function (response) {
alert("Success");
},
error: function () {
alert("Error in Ajax");
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
另外,JSON会自动反序列化服务器端,所以你应该期望name,pk并value在服务器端的参数(这就是插件是根据发送文档)
[System.Web.Services.WebMethod]
public static String TestMethod(string name, string pk, string value)
{
//access params here
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,pk由于您没有设置,因此将为null.
| 归档时间: |
|
| 查看次数: |
5668 次 |
| 最近记录: |