tags:

views:

75

answers:

2

There are two methods GetUserAssignedSystems() and GetUserAssignedSystems(string Id) These methods act very differently from each other. The problem is, when I want to call GetUserAssignedSystems(string Id), the parameter-less method is called.

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:

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

Here is the code for the 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
});

}

+4  A: 

Javascript does not support overloaded functions (multiple functions with the same name that accept different parameters) in the same way that some languages do. 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. 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. Let me know how that behaves.

g.d.d.c
Thanks! I didn't realize that you could not have overloaded methods. When I commented out the parameter-less method everything worked perfect. Thanks again!
Avien
Certainly! Glad to help.
g.d.d.c
@g.d.d.c. But is seems like the failure to call the right function is happening at the C# level, not JS. I am not sure removing the other function fixed the problem or simply masked it.
Doug Neiner
@Doug - I was about to go out and buy a new javascript book because it sure didn't look like js to me. Unless perhaps those functions generate equivalent javascript functions that are sent to the client.
patrick dw
@doug-neiner Can you elaborate a little bit further? I'm not terribly familiar with the correlation between the C# functions and your JS functions. Does CallMfttService launch an AJAX Request and pass in the parameters to your C# function? Can your C# function be modified to contain checking for the presence of the cacID variable? C#'s not my strong suit per se, but if you can provide some more code I can try to help further.
g.d.d.c
@doug-neiner Not sure if me changing the post updates you, but I know this comment will flag you. I think the trouble was with how you were passing the parameters to jQuery's ajax method. Thanks,
g.d.d.c
A: 

I am not sure if you mean to send JSON, or a normal object literal. But in the first case, your JSON is invalid... JSON needs double quotes around strings and keys:

 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("ServiceLayer/UserManager.asmx/GetUserAssignedSystems", {cacId:   $('#EditUserCacId').val()}, function(result) {
Doug Neiner