1

您好,我希望能够向我在 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>

我试着做:

data: data + "&moredata=" + morevalue 

但是我收到一个脚本错误,它不会将它发送到我的 URL...请帮助!

编辑1:

我现在正在传递它,就像这个 DPID 很好但是 dt 没有:

<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>
4

3 回答 3

2

Smiter 的解决方案对我不太适用。查看对象的结构,aoData您需要创建一个带有namevalue字段的对象:

"fnServerParams": function (aoData) {
    aoData.push({
        name: "Name",
        value: "Value"
    });
},
于 2013-10-17T11:38:28.033 回答
1

如果要向 ajax 请求添加一些额外的数据,请使用:

"fnServerParams": function ( aoData ) {
                aoData.push( { "key": "value" } );
 }
于 2012-10-23T07:15:27.250 回答
0

数据是一个对象,所以你可以这样做:

data.moredata=morevalue

您需要在成功处理程序中执行此操作或根据需要将数据作为参数传递

编辑1:

现在我已经看到了你的信息,试试这个:

"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.");
                }
            });
        }
于 2012-10-22T22:08:45.293 回答