简体   繁体   English

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

[英]Two methods with different signatures, jquery is not calling correct method

There are two methods GetUserAssignedSystems() and GetUserAssignedSystems(string Id) These methods act very differently from each other. 有两种方法GetUserAssignedSystems()GetUserAssignedSystems(string Id)这些方法的行为彼此截然不同。 The problem is, when I want to call GetUserAssignedSystems(string Id) , the parameter-less method is called. 问题是,当我想调用GetUserAssignedSystems(string Id) ,会调用无参数方法。

Here are the methods: 以下是方法:

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

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

Here is the jQuery making the call: 这是调用jQuery的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');
            }
        });

Any ideas why this method is being ignored? 有什么想法为什么忽略这种方法?

UPDATE UPDATE

Here is the code for the CallMfttService 这是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 does not support overloaded functions (multiple functions with the same name that accept different parameters) in the same way that some languages do. Javascript不支持重载函数(具有接受不同参数的同名的多个函数),就像某些语言一样。 If you need it to optionally perform tasks based on the presence of an input parameter you have to add checking within the function that determines if the variable was passed if ( param == undefined) {} and then behave in the appropriate way based on the presence or absence of said variable. 如果你需要它来根据输入参数的存在选择性地执行任务,你必须在函数中添加检查,以确定变量是否被传递if ( param == undefined) {}然后以适当的方式基于是否存在所述变量。 Redefining the function twice with different parameters won't work. 使用不同的参数重新定义该函数两次将不起作用。

Per updates, try changing your call like so: 每次更新,请尝试更改您的通话,如下所示:

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

Essentially you were passing a string, not an object to the jQuery $.ajax method, which was probably preventing your values from reaching the server properly. 基本上你是传递一个字符串,而不是一个jQuery $ .ajax方法的对象,这可能会阻止你的值正确到达服务器。 Let me know how that behaves. 让我知道这是怎么回事。

I am having a similar issue where the correct C# WebMethod is not getting called. 我遇到了一个类似的问题,即没有调用正确的C#WebMethod。 I am getting a "Resource Not Found Error". 我收到“资源未找到错误”。 It appears that for whatever reason (I am not sure why exactly, maybe someone else can elaborate), the WebMethod Class does not like overloaded methods. 似乎无论出于何种原因(我不确定为什么,也许其他人可以详细说明),WebMethod类不喜欢重载方法。 It is incorrectly interpreting the type of the passed parameters. 它错误地解释了传递参数的类型。 If I remove all of the overloaded methods in the C# WebMethod Class and leave just one, regardless of which one I leave behind, it will now get called and work as expected. 如果我删除C#WebMethod类中的所有重载方法并只保留一个,无论我留下哪一个,它现在都会被调用并按预期工作。

I am not sure if you mean to send JSON, or a normal object literal. 我不确定你是想发送JSON还是普通的对象文字。 But in the first case, your JSON is invalid... JSON needs double quotes around strings and keys: 但在第一种情况下,您的JSON无效...... JSON需要围绕字符串和键的双引号:

 CallMfttService("ServiceLayer/UserManager.asmx/GetUserAssignedSystems", '{"cacId":"' + $('#EditUserCacId').val() + '"}', function(result) {

Without knowing what CallMfttService does, its hard to say, but I would def try this as well: 不知道CallMfttService做了什么,很难说,但我也会尝试这样做:

CallMfttService("ServiceLayer/UserManager.asmx/GetUserAssignedSystems", {cacId:   $('#EditUserCacId').val()}, function(result) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从接收委托类型参数的方法中调用具有不同签名的方法 - Calling methods with different signatures from a method that receives a delegate type parameter 两种不同签名的一种方法 - One method for two different signatures 如何结合签名略有不同的两种方法? - How to combine two methods with slightly different signatures? 继承具有相同方法但签名不同的多个接口的正确方法? - Correct way to Inherit multiple interfaces with same methods but different signatures? 具有不同签名的控制器操作方法 - Controller Action Methods with different signatures 如何在 .NET 中创建通用方法来捕获应用程序中具有不同签名的不同方法的延迟(经过时间)? - How to create a generic method in .NET to capture latency (elapsed times) of different methods with different signatures in my application? 在数据访问方法中调用两个方法的问题 - Issue with calling two methods in data access method 用于不同方法签名的“三元”运算符 - “Ternary” operator for different method signatures 重构方法调用具有不同参数签名的代理 - Refactor methods invoking delgates with different parameter signatures 锁定两种方法并确保仅在线程上调用一种方法 - Locking two methods and ensure that only on thread is calling one method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM