Undefined key for sending additional data to server
Undefined key for sending additional data to server
Hi,
I'm trying to send additional data to server to return my json data.
[code]
$('#submit').click(function(){
var from = $('#from').val();
var to = $('#to').val();
var loadURL = 'queryAJAX.php';
oTable = $('#specificDateTable').dataTable({
"bProcessing": true,
"sAjaxSource": loadURL,
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push(
{"key":"from_date","value":from},
{"key":"to_date","value":to}
);
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
});
[/code]
When I check with firebug, I noticed that the value is sent, but the key show undefined
[code]
undefined 2011-07-05
undefined 2011-08-09
[/code]
Could you help me where did I go wrong?
Thanks
Regards,
I'm trying to send additional data to server to return my json data.
[code]
$('#submit').click(function(){
var from = $('#from').val();
var to = $('#to').val();
var loadURL = 'queryAJAX.php';
oTable = $('#specificDateTable').dataTable({
"bProcessing": true,
"sAjaxSource": loadURL,
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push(
{"key":"from_date","value":from},
{"key":"to_date","value":to}
);
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
});
[/code]
When I check with firebug, I noticed that the value is sent, but the key show undefined
[code]
undefined 2011-07-05
undefined 2011-08-09
[/code]
Could you help me where did I go wrong?
Thanks
Regards,
This discussion has been closed.
Replies
Allan
My problem now is
[code]$('#submit').click(function(){
var from = $('#from').val();
var to = $('#to').val();
var loadURL = 'queryAJAX.php';
if(typeof oTable == 'undefined'){
oTable = $('#specificDateTable').dataTable({
"bProcessing": true,
"bRetrieve": true,
"sAjaxSource": loadURL,
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push(
{"name":"from","value":from},
{"name":"to","value":to}
);
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
}else{
//Reload the table with new data
}
});[/code]
I'm thinking of using fnReloadAjax to reload the table, my question is within the code of fnReloadAjax
[code]oSettings.fnServerData( oSettings.sAjaxSource, [], function(json) {
/* Clear the old information from the table */
that.oApi._fnClearTable( oSettings );
/* Got the data - add it to the table */
for ( var i=0 ; i
Move your 'from' and 'to' code into the fnServerData so that it grabs the current value each time this server call is made.
[code]
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push(
{"name":"from","value":$('#from').val()},
{"name":"to","value":$('#to').val()}
);
[/code]
[code]
// make sure your oTable variable is defined globally or otherwise some way you can retrieve it rather than just defining it within your event handler
var oTable;
$(document).ready(function() { // initialize the oTable as soon as the DOM is ready
oTable = $('#specificDateTable').dataTable({
// ...
});
$('#submit').click(function(){ oTable.fnDraw() }); // or simply $('#submit').click(oTable.fnDraw);
});
[/code]
My table needs data from click submit button to be displayed at the 1st time.
From your suggestion, I change the order of the code
[code]
var oTable;
$(document).ready(function() {
$('#submit').click(function(){
if(typeof oTable == 'undefined'){
var loadURL = 'queryAJAX.php';
oTable = $('#specificDateTable').dataTable({
"bProcessing": true,
"bRetrieve": true,
"sAjaxSource": loadURL,
"fnServerData": function ( sSource, aoData, fnCallback ) {
aoData.push(
{"name":"from","value":$('#from').val()},
{"name":"to","value":$('#to').val()}
);
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
}else{
alert("Test");
oTable.fnDraw();
}
});
});
[/code]
When the submit button is clicked, browser alerts "test", but I see no data being sent from Firebug, can you help me with this?
Thank you so much!
Best Regards,
Gerard.
Or view the debugger under XHR. You can view the headers, response, JSON, etc. to see what's up
http://i.imgur.com/VnNXs.png (screen capture of FireBug)
Allan
I'm currently doing the code in localhost.
I did try to alert and log the value of 'from' and 'to' to the console, all are displayed
[code] alert($('#from').val());
alert($('#to').val());
console.log($('#from').val());
console.log($('#to').val());[/code]
http://i179.photobucket.com/albums/w293/blackstorm_9x/log-1.jpg
Like I said, the table did display for the 1st round, from 2nd round, there is no data sent to the server
Thank you for helping me!
Gerard.
Allan
Thank you so much!
Best Regards,
Gerard.