We help IT Professionals succeed at work.

Send parameters via datatable ajax call

Mauro Cazabonnet
on
5,719 Views
Last Modified: 2017-04-27
I'm trying to send a json string param to a post rest webapi method

Any assistance much appreciated


this doesn't work

var report = 2;
    var parameters = "{'report':'" + report + "'}";
   
    implementationworkqueueTable = $("#implementationworkqueueTable").DataTable({
        dom: 'Blfrtip',
        oSearch: { "bRegex": false, "bSmart": true },
        deferRender: true,
        bFilter: true,
        bSearchable: true,
        bProcessing: true,
        bRetrieve: true,
        bSortable: true,
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        stripeClasses: [],
        ajax: {
            "url": "api/InfoHedge/IRequest",
            "async": true,
            "data": parameters,
            "dataSrc": function (json) {
                var data = JSON.parse(json);
                alert(json);
            },
            "datatype": "json",
            "contenttype": "application/json; charset=utf-8",
            "type": "POST"
        }
    });

Open in new window


This works

var report = 2;
    var parameters = "{'report':'" + report + "'}";

    $.ajax({
        type: "POST",
        async: true,
        url: "api/InfoHedge/IRequest",
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var data = JSON.parse(result);

            var test = JSON.stringify(data.Table);
            alert(test);
            //alert(result);
        }
    });

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mauro CazabonnetSenior Windows Automation Developer
CERTIFIED EXPERT
Top Expert 2015

Author

Commented:
It's hitting the server side method

but the parameter is not being passed...

//POST: api/InfoHedge/IRequest --- Submit Implementation request
        [Route("IRequest")]
        [HttpPost]
        public string Implementation([FromBody] dynamic data)
        {
            try
            {
                object result = null;
                int report = data.report;
              
                var connectString = WebConfigurationManager.ConnectionStrings["IHDBSRV106"].ConnectionString;
                using (SqlConnection sqlconn = new SqlConnection(connectString))
                {
                    using (System.Data.SqlClient.SqlCommand sqlCmd = sqlconn.CreateCommand())
                    {
                        if (report == 1)
                        {
                            string servertype = data.servertype;
                            string serversite = data.serversite;
                            string domainname = data.domainname;
                            string tldomainsuffix = data.tldomainsuffix;
                            string domainnetbios = data.domainnetbios;
                            int clientthirdoctet = data.clientthirdoctet;
                            int clientlocaloctet = data.clientlocaloctet;
                            int clientsslthirdoctet = data.clientsslthirdoctet;
                            int clientvlan = data.clientvlan;
                            string companyname = data.companyname;
                            string state = data.state;
                            string city = data.city;
                            string streetaddress = data.streetaddress;
                            string postalcode = data.postalcode;

                            sqlCmd.CommandText = "sp_Implementation";
                            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                            sqlCmd.CommandTimeout = 0;
                            sqlCmd.Parameters.AddWithValue("@report", DbType.Int32).Value = report;
                            sqlCmd.Parameters.AddWithValue("@servertype", DbType.String).Value = servertype;
                            sqlCmd.Parameters.AddWithValue("@serversite", DbType.String).Value = serversite;
                            sqlCmd.Parameters.AddWithValue("@domainname", DbType.String).Value = domainname;
                            sqlCmd.Parameters.AddWithValue("@tldomainsuffix", DbType.String).Value = tldomainsuffix;
                            sqlCmd.Parameters.AddWithValue("@domainnetbios", DbType.String).Value = domainnetbios;
                            sqlCmd.Parameters.AddWithValue("@clientthirdoctet", DbType.Int32).Value = clientthirdoctet;
                            sqlCmd.Parameters.AddWithValue("@clientlocaloctet", DbType.Int32).Value = clientlocaloctet;
                            sqlCmd.Parameters.AddWithValue("@clientsslthirdoctet", DbType.Int32).Value = clientsslthirdoctet;
                            sqlCmd.Parameters.AddWithValue("@clientvlan", DbType.Int32).Value = clientvlan;
                            sqlCmd.Parameters.AddWithValue("@companyname", DbType.String).Value = companyname;
                            sqlCmd.Parameters.AddWithValue("@state", DbType.String).Value = state;
                            sqlCmd.Parameters.AddWithValue("@city", DbType.String).Value = city;
                            sqlCmd.Parameters.AddWithValue("@streetaddress", DbType.String).Value = streetaddress;
                            sqlCmd.Parameters.AddWithValue("@postalcode", DbType.String).Value = postalcode;
                            sqlconn.Open();

                            DataSet ds = new DataSet();
                            SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                            da.Fill(ds);
                            result = ds;
                        }

                        if (report == 2)
                        {
                            sqlCmd.CommandText = "sp_Implementation";
                            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                            sqlCmd.CommandTimeout = 0;
                            sqlCmd.Parameters.AddWithValue("@report", DbType.Int32).Value = report;
                            sqlconn.Open();

                            DataSet ds = new DataSet();
                            SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                            da.Fill(ds);
                            result = ds;
                        }

                        return JsonConvert.SerializeObject(result);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return JsonConvert.SerializeObject(ex.Message);
                
            }
        }
    }

Open in new window

Mauro CazabonnetSenior Windows Automation Developer
CERTIFIED EXPERT
Top Expert 2015

Author

Commented:
This worked for me :)

var report = 2;
    var parameters = JSON.stringify("{'report':'" + report + "'}");

    implementationworkqueueTable = $("#implementationworkqueueTable").DataTable({
        dom: 'Blfrtip',
        oSearch: { "bRegex": false, "bSmart": true },
        deferRender: true,
        bFilter: true,
        bSearchable: true,
        bProcessing: true,
        bRetrieve: true,
        bSortable: true,
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
        stripeClasses: [],
        ajax: {
            "url": "api/InfoHedge/IRequest",
            "contentType": "application/json; charset=utf-8",
            "type": "POST",
            "data": function (d) {
                return parameters;
            },
        success: function (result) {
            var data = JSON.parse(result);
            alert(result);
            }
        }
    });

Open in new window


Thx for the assist....
Mauro CazabonnetSenior Windows Automation Developer
CERTIFIED EXPERT
Top Expert 2015

Author

Commented:
thx......
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.