3

我在 Asp.net 中使用X-Editable 插件。
我试过这个:Usage with .Net and 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");
           }
          });
      }
});

HTML:

<a href="#" id="username" class="myeditable">superuser</a>

Default.aspx 中的 WebMethod:

[System.Web.Services.WebMethod]
public static String TestMethod(String params)
{
    //access params here
}
4

1 回答 1

5

如果要调用页面方法,首先需要发出POST类型的请求(同时设置 content-type 也不会造成任何伤害):

$('#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");
            }
        });
    }
});

此外,JSON 将在服务器端自动反序列化,因此您应该期待服务器端的name,pkvalue参数(这是插件根据文档发送的内容)

[System.Web.Services.WebMethod]
public static String TestMethod(string name, string pk, string value)
{
    //access params here
}

在您的情况下,pk由于您没有设置,因此将为 null 。

于 2012-12-10T08:45:58.773 回答