c# - 两种具有不同签名的方法,jquery 没有调用正确的方法

标签 c# jquery

有两种方法 GetUserAssignedSystems()GetUserAssignedSystems(string Id) 这些方法的作用非常不同。问题是,当我想调用 GetUserAssignedSystems(string Id) 时,调用了无参数方法。

方法如下:

[WebMethod]
[ScriptMethod]
public IEnumerable GetUserAssignedSystems(string cacId)
{
    return Data.UserManager.GetUserAssingedSystems(cacId);
}

[WebMethod]
[ScriptMethod]
public IEnumerable GetUserAssignedSystems()
{
    //do something else
}

这是进行调用的 jQuery:

CallMfttService("ServiceLayer/UserManager.asmx/GetUserAssignedSystems", 
        "{'cacId':'" + $('#EditUserCacId').val() + "'}", function(result) {
            for (var userSystem in result.d) {
                $('input[UserSystemID=' + result.d[userSystem] + ']').attr(
                    'checked', 'true');
            }
        });

知道为什么这个方法被忽略了吗?

更新

这是 CallMfttService 的代码

function CallMfttService(method, jsonParameters, successCallback, errorCallback){
if (errorCallback == undefined)
{
    errorCallback = function(xhr)
    {
        if (xhr.status == 501)
        {
            alert(xhr.statusText);
        }
        else
        {
            alert("Unexpected Error");
        }
    }
}

$.ajax({
    type: "POST",
    url: method,
    data: jsonParameters,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successCallback,
    error: errorCallback
});

最佳答案

Javascript 不像某些语言那样支持重载函数(具有相同名称但接受不同参数的多个函数)。如果您需要它根据输入参数的存在有选择地执行任务,您必须在函数中添加检查以确定是否传递了变量 if (param == undefined) {} 然后执行根据所述变量的存在或不存在以适当的方式。用不同的参数重新定义函数两次是行不通的。

根据更新,尝试像这样更改您的调用:

CallMfttService("ServiceLayer/UserManager.asmx/GetUserAssignedSystems", 
    {'cacId': $('#EditUserCacId').val() }, function(result) {
        for (var userSystem in result.d) {
            $('input[UserSystemID=' + result.d[userSystem] + ']').attr(
                'checked', 'true');
        }
    });

本质上,您向 jQuery $.ajax 方法传递的是一个字符串,而不是一个对象,这可能会阻止您的值正确到达服务器。让我知道它的行为方式。

关于c# - 两种具有不同签名的方法,jquery 没有调用正确的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3046916/

相关文章:

c# - Sprite 旋转 XNA

c# - 使用数据绑定(bind)和线程关闭 WPF 应用程序

javascript - 如何在没有 Flash 的情况下制作交互式城市 map

javascript - 显示消息

javascript - Ext.NET 中的复选框问题

用于代码 View 的 javascript 库?

c# - 如何以编程方式为特定用户控件使用输出缓存?

C# UserPrincipal - ChangePassword 执行策略但 SetPassword 不执行策略?

c# - 如何使用 Facebook C# SDK 登录 Facebook

java - 使用jtable和jquery插件进行Crud操作