使用jquery ajax调用C#webmethod省略可选参数

时间:2014-05-27 10:34:48

标签: c# javascript jquery ajax

我有一个像这样的网络方法:

[WebMethod(EnableSession = true)]
public string testMethod(string param1, string param2="default value")
{
    .......
}

我正在尝试使用jquery省略可选参数来调用此方法,并且webmethod不会被命中。

$.ajax({
    type: "POST",
    url: url,
    data: { "param1": "value1" }
}).complete(function (data) {....});  

我只有在传递第二个参数时才能工作。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

我认为你不能使用 Optional Params 和webservice方法,你可能需要重载方法......

[WebMethod(EnableSession = true, MessageName = "testMethod"))]
public string testMethod(string param1, string param2)
{
      .......
}

[WebMethod(EnableSession = true, MessageName = "testMethod")]
public string testMethod(string param1)
{
      testMethod(param1, "default value");
}

编辑:文章链接。

还有一个article here值得阅读其他方法,虽然它最终没有使用重载,但我想这是你的决定。