How do use pass parameter in dataSrc DataTables ?

How do use pass parameter in dataSrc DataTables ?

headshot9xheadshot9x Posts: 59Questions: 16Answers: 1
edited May 2015 in Free community support

Hello everybody.I have a small question as follow , I use the parameter passed dataSrc AJAX . I have a function that takes on values and returns JSON string parameter then I must declare how you use dataSrc. In my function GET_PRODUCT input a parameter as string @product_name . The result as JSON , I do not know how to pass parameters in here .

 "ajax": {
                    "url": "../BUS/WebService.asmx/GET_PRODUCT",
                    "dataType": "json",
                    "contentType": "application/json; charset=utf-8",
                    "type": "POST",
                    dataSrc: function (json) {
                        return $.parseJSON(json.d);
                    }
                    //dataSrc: function (json(candy)) {
                    //    return $.parseJSON(json.d);
                    //}
                    //dataSrc: "Candy",
                },

Thank you.

This question has an accepted answers - jump to answer

Answers

  • headshot9xheadshot9x Posts: 59Questions: 16Answers: 1

    I try , but it is not work

    "ajax": {
                    "url": "../BUS/WebService.asmx/GET_PRODUCT",
                    "dataType": "json",
                    "contentType": "application/json; charset=utf-8",
                    "type": "POST",
                    //dataSrc: function (json) {
                    //    return $.parseJSON(json.d);
                    //}
                    "dataSrc": function (d) {
               //parameter product_name of WebMethod GET_PRODUCT , then convert result to JSON string
                        return $.parseJSON($.extend({}, d, { "product_name": "Candy" }));
                    }
                },
    
    Message: "Invalid web service call, missing value for parameter: 'product_name'."
    

    Thank you.

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin

    Can you link to the page in question showing the problem so I can debug it please.

    Allan

  • headshot9xheadshot9x Posts: 59Questions: 16Answers: 1
    edited May 2015

    @, I try

    var table;
                table = $('#div_table').DataTable({
                    "processing": false,
                    "serverSide": false,
                    "ajax": {
                        "url": "../BUS/WebService.asmx/GET_PRODUCT",
                        "dataType": "json",
                        "contentType": "application/json; charset=utf-8",
                        "type": "POST",
                        "data": function (d) {
                            return $.extend({}, d, {
                                "product_name": "Candy"
                            });
                        }
                    },
    

    After run , i get error
    {"Message":"Invalid JSON primitive: product_name.","StackTrace":"

  • headshot9xheadshot9x Posts: 59Questions: 16Answers: 1
    edited May 2015

    Dear allan . After try more time .I get

      "data": function (data) { return "{'product_name':'Candy'}"; }
    

    It have just worked and the result as JSON string , But it not show in DataTables. It error show at file jquery.datatable.js

    Uncaught TypeError: Cannot read property 'length' of undefined
    

    I think the result should parseJSON

     var table;
                table = $('#div_table').DataTable({
                    "processing": false,
                    "serverSide": false,
                    "ajax": {
                        "url": "../BUS/WebService.asmx/GET_PRODUCT",
                        "dataType": "json",
                        "contentType": "application/json; charset=utf-8",
                        "type": "POST",
                        "data": function (data) { return $.parseJSON("{'product_name':'Candy'}"); }
                    },
    

    How do you think it ?.Thank sir

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin

    I don't really know anything about your server-side platform so I can't really give much advice about it. You can certainly do that in ajax.data - I don't really see why it would be needed, but if it works for your use case...

    Allan

  • headshot9xheadshot9x Posts: 59Questions: 16Answers: 1
    edited May 2015

    As i say "I can get data" when using

     var table;
                table = $('#div_table').DataTable({
                    "processing": false,
                    "serverSide": false,
                    "ajax": {
                        "url": "../BUS/WebService.asmx/GET_PRODUCT",
                        "dataType": "json",
                        "contentType": "application/json; charset=utf-8",
                        "type": "POST",
                        "data": function (data) { return "{'product_name':'Candy'}"; }
                    },
    

    but it's not show in DataTables, please, see at http://debug.datatables.net/ibexok ., you will see data , but it's not show in DataTables.
    I'm so sory , i try the function orther in this case

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin
    edited May 2015 Answer ✓

    The server is responding with a single parameter in the JSON object called d. I have no idea why Microsoft thought that would be a good idea, but it is nonsense!

    You need to use the ajax.dataSrc option to parse the JSON contains in that parameter and then return it. For example

    dataSrc: function ( json ) {
      return $.parseJSON( json.d );
    }
    

    Allan

This discussion has been closed.