将数据添加到.Ajax数据+额外数据
您好我希望能够在Ajax上提交的数据中添加其他数据:
$(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."); } }); } }); });
我尝试过:
data: data + "&moredata=" + morevalue
但是我收到一个脚本错误,它不会发送到我的URL …请帮忙!
编辑1:
我现在正在传递它,就像这个DPID遇到了好但是dt没有:
$(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."); } }); } }); });
如果要向ajax请求添加一些其他数据,请使用以下命令:
"fnServerParams": function ( aoData ) { aoData.push( { "key": "value" } ); }
Smiter的解决方案并不适合我。 查看aoData
中对象的结构,您需要创建一个具有name
和value
字段的对象:
"fnServerParams": function (aoData) { aoData.push({ name: "Name", value: "Value" }); },
data是一个Object,所以你可以这样做:
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."); } }); }