如何简化参数的传递

时间:2018-01-05 04:47:56

标签: javascript c#

我在c#中有这些方法需要3个参数

public void Delete_AgentTools(int ID,int UAM,int mode)
    {
       some code etc.
    }

我使用javascript ajax调用此方法并传递参数

function Delete_AgentTools(toolAccess, toolId, UAM) {
$.ajax({

    type: "POST",
    url: "IROA_StoredProcedures.asmx/Delete_AgentTools",
    data: "{'toolAccess':'" + toolAccess + "', 'toolId':'" + toolId + "', 'UAM':'" + UAM + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success:function()
    {
        alert("Tool has been successfully delete");
    },
    error: function (XMLHttpRequest)
    {
        alert("error in Delete_AgentTools()");
        console.log(XMLHttpRequest);
    }

});
}

你看,我想简化我如何在javascript中传递参数。可以将其作为object传递给c#,还是简化parametersjavascript的传递

2 个答案:

答案 0 :(得分:3)

您可以使用pop.list <-list(1:10)

将js对象转换为JSON
JSON.stringify

答案 1 :(得分:1)

function Delete_AgentTools(toolAccess, toolId, UAM) {
    var data = {};
    data.mode = toolAccess;
    data.ID = toolId;
    data.UAM = UAM;

    $.ajax({

        type: "POST",
        url: "IROA_StoredProcedures.asmx/Delete_AgentTools",
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success:function()
        {
            alert("Tool has been successfully delete");
        },
        error: function (XMLHttpRequest)
        {
            alert("error in Delete_AgentTools()");
            console.log(XMLHttpRequest);
        }
    });

无需更改C#代码。