0

I have these method in c# which requires 3 parameters

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

and I use javascript ajax to call this method and pass the parameter

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

});
}

you see for me I want to simpilfy on how I pass the parameter in javascript. Is it possible to pass it as an object to the c# or simplify the passing of parameters in javascript

Vian Ojeda Garcia
  • 827
  • 4
  • 17
  • 34
  • 2
    You don't want to manually create JSON, that's a recipe for disaster. Look into `JSON.stringify`. You can get JSON from a JS object. – elclanrs Jan 05 '18 at 04:49
  • Possible duplicate of [Serializing an object to JSON](https://stackoverflow.com/questions/558518/serializing-an-object-to-json) – ProgrammingLlama Jan 05 '18 at 04:51

2 Answers2

3

You can convert a js object as a JSON using JSON.stringify

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

});
Kenry Sanchez
  • 1,703
  • 2
  • 18
  • 24
  • I want if possible to use class or object in parameters of c# – Vian Ojeda Garcia Jan 05 '18 at 05:12
  • You don't need to make any change in your c# code. But, if you want to do something like a destructuring assignment in your c# code, maybe you could make a model class with all your properties and just serialize your C# class. – Kenry Sanchez Jan 05 '18 at 13:58
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);
        }
    });

No need to change in your C# code.

Yogesh Patel
  • 818
  • 2
  • 12
  • 26