有两种方法 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/