kevin c
kevin c

Reputation: 795

Add Data to .Ajax data + extra data

Hello I want to be able to add additional data to my data being submitted on Ajax:

<script type="text/javascript">
$(document).ready(function() {
    //http://www.datatables.net
$('#dataTable').dataTable({
        "sPaginationType": "full_numbers",
        "bJQueryUI": false,
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/Project/GetDataTables2",
        "aoColumns": [
            { "bSortable": true },
            { "bSortable": true },
            { "bSortable": true },
            { "bSortable": true }
        ],
        "fnServerData": function(url, data, callback) {
            $.ajax({
                "url": url,
                "data": data, //I want to add additional data from here like the QueryString DPID
                "success": callback,
                "contentType": "application/x-www-form-urlencoded; charset=utf-8",
                "dataType": "json",
                "type": "POST",
                "cache": false,
                "error": function() {
                    alert("DataTables warning: JSON data from server failed to load or be parsed. " +
                    "This is most likely to be caused by a JSON formatting error.");
                }
            });
        }
    });
});                    
</script>

I tried doing:

data: data + "&moredata=" + morevalue 

However I get a script error and it won't send it to my URL... Please help!

Edit 1:

I am now passing it like this DPID comes across fine but dt does not:

<script type="text/javascript">
$(document).ready(function() {
    //http://www.datatables.net
    $('#dataTable').dataTable({
        "sPaginationType": "full_numbers",
        "bJQueryUI": false,
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/Project/GetDataTables2",
        "aoColumns": [
            { "bSortable": true },
            { "bSortable": true },
            { "bSortable": true },
            { "bSortable": true }
        ],
        "fnServerData": function(url, data, callback) {
            $.ajax({
                "url": url,
                "data": { DPID: "1", dt: data }, //I want to add additional data from here like the QueryString DPID
                "success": callback,
                "contentType": "application/x-www-form-urlencoded; charset=utf-8",
                "dataType": "json",
                "type": "POST",
                "cache": false,
                "error": function() {
                    alert("DataTables warning: JSON data from server failed to load or be parsed. " +
                    "This is most likely to be caused by a JSON formatting error.");
                }
            });
        }
    });
});                    
</script>

Upvotes: 1

Views: 5665

Answers (3)

weenoid
weenoid

Reputation: 1186

Smiter's solution didn't quite work for me. Looking at the structure of objects in aoData you need to create an object with name and value fields:

"fnServerParams": function (aoData) {
    aoData.push({
        name: "Name",
        value: "Value"
    });
},

Upvotes: 2

Smiter
Smiter

Reputation: 1391

if you want to add some additional data to the ajax request, use this:

"fnServerParams": function ( aoData ) {
                aoData.push( { "key": "value" } );
 }

Upvotes: 1

user1026361
user1026361

Reputation: 3657

data is an Object, so you can do:

data.moredata=morevalue

You'll need to do it within your success handler or pass data as a parameter as needed

EDIT 1:

Now that I've seen your info, try this:

"fnServerData": function(url, data, callback) {
           data={ DPID: "1", dt: data } 
           $.ajax({
                "url": url,
                "data": data, //I want to add additional data from here like the QueryString DPID
                "success": callback,
                "contentType": "application/x-www-form-urlencoded; charset=utf-8",
                "dataType": "json",
                "type": "POST",
                "cache": false,
                "error": function() {
                    alert("DataTables warning: JSON data from server failed to load or be parsed. " +
                    "This is most likely to be caused by a JSON formatting error.");
                }
            });
        }

Upvotes: 0

Related Questions