如何简化参数的传递

我有这些方法在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#或简化在javascript传递parameters


您可以使用JSON.stringify将js对象转换为JSON

var data = {};
data.toolAccess = value1;
data.toolId = value2;
data.UAM = value3;

$.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);
    }

});

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#代码中进行更改。

链接地址: http://www.djcxy.com/p/46285.html

上一篇: How to simplfy the passing of parameter

下一篇: converting object key value pairs to JSON