How to send a parameter to the server with datatables and .NET WCF?

How to send a parameter to the server with datatables and .NET WCF?

kerberonixkerberonix Posts: 7Questions: 5Answers: 0

I have some working code here which gets data from the server and puts it into DataTables.

However I need to expand this code to provide a parameter to the ajax request to help the server obtain the right data. Does anyone know how to do this using datatables and a WCF (.NET) service?

The working code which just gets the data without the parameter is below

        $("#transactionTable").DataTable({

        // example value for the parameter I need to send
        var date = "12/07/2010";

            "processing": true,
            type: "GET",
            ajax: {
                url: "Service.svc/GetData",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                dataSrc: "",
                data: JSON.stringify(date)
            },
            "columns": [
                { data: "CustomerName" },
                { data: "CompanyName" },
                { data: "CurrentBalance" }
            ]
        });

WCF Service

[OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    public List<JsonObject> GetData(string date) // this is null at the moment, it is never passed
    {
        jsonObjects = new List<JsonObject>();
        var getJsonObjects = dbConnection.GetDbData(jsonObjects, date);
        return getJsonObjects;
    }

I know that I probably need to modify this to a POST request but it throws an error when I try this.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @kerberonix ,

    You can pass parameters to the Ajax request by using ajax.data, as shown in this example here.

    Hope that helps,

    Cheers,

    Colin

This discussion has been closed.