0

我有一个带有 4 个参数的 JavaScript 函数

function GetEmpDetails(EmpID, ApplicationID, ActionID, ApplicationSubID) 
{  
  $.ajax({
    type: "POST",
    url: '/TransController/GetEmpInformation',
    data: { "EmpID": JSON.stringify(EmpID), "ApplicationID": ApplicationID, "ActionID": ActionID, "ApplicationSubID": ApplicationSubID },
    dataType: "json",
    beforeSend: function () {
    },
    success: function (data) {
        DisplayDetails(data);
    },
    error: function (result) {
    }
  });
}

我想用一个不同的参数覆盖相同的函数。如下所示

function GetEmpDetails(EmpID, ApplicationID, ActionID, DateOfJoining) 
{  
  $.ajax({
    type: "POST",
    url: '/TransController/GetEmpInformation',
    data: { "EmpID": JSON.stringify(EmpID), "ApplicationID": ApplicationID, "ActionID": ActionID, "DateOfJoining": DateOfJoining},
    dataType: "json",
    beforeSend: function () {
    },
    success: function (data) {
        DisplayDetails(data);
    },
    error: function (result) {
    }
  });
}

有人请告诉,是否有可能用一些示例代码来做以及如何做?

4

2 回答 2

0

您可以将数据作为对象传递

function GetEmpDetails(data) 
{  
  $.ajax({
    type: "POST",
    url: '/TransController/GetEmpInformation',
    data: data,
    dataType: "json",
    beforeSend: function () {
    },
    success: function (data) {
        DisplayDetails(data);
    },
    error: function (result) {
    }
  });
}

用法

GetEmpDetails({EmpID: "", ...,  ApplicationSubID: ""})
GetEmpDetails({EmpID: "", ...,  Whatever: ""})
于 2013-11-07T06:35:34.420 回答
0

如何使用附加参数(非常类似于 python 的**kwargs?

function GetEmpDetails(EmpID, ApplicationID, ActionID, AdditionalParameters) 
{  
  var data = AdditionalParameters;
  $.extend(data, { "EmpID": JSON.stringify(EmpID), "ApplicationID": ApplicationID, "ActionID": ActionID})
  $.ajax({
    type: "POST",
    url: '/TransController/GetEmpInformation',
    data: data, 
    dataType: "json",
    beforeSend: function () {
    },
    success: function (data) {
        DisplayDetails(data);
    },
    error: function (result) {
    }
  });
}

现在以以下两种方式之一调用您的函数:

GetEmpDetails(1, 1, 1, {"DateOfJoining": DateOfJoining});
GetEmpDetails(1, 1, 1, {"ApplicationSubID": 1});
于 2013-11-07T06:35:54.280 回答