How to pass parameters for ajax call

How to pass parameters for ajax call

kulkarni_ashkulkarni_ash Posts: 35Questions: 8Answers: 0
edited February 2012 in General
I have a function like below which is working fine, now i want pass some parameters to my spring controller how can i do so

The controller is like below

[code]
@RequestMapping(value = "/displayDataAjax")
public @ResponseBody
DataTablesResponse displayDataAjax(@RequestParam
int year, @RequestParam String code) throws Exception {
//logic to send JSON here
}

[/code]

My Ajax call to populate data table,
[code]
$('#example').dataTable({
"bSort": false,
"sPaginationType": "full_numbers" ,
"bProcessing": true,
"bDeferRender": true,
"bDestroy": true,
"sAjaxSource": 'displayDataAjax',
"aoColumns": [

{
"fnRender": function ( oObj ) {
var text = "";

return text;
}

}
,
{
"fnRender": function ( oObj ) {

// alert($('#editAccess').val());
if($('#editAccess').val() == 'true'){
var text = "" + oObj.aData.airportcd + "";
return text;
}
else{
return oObj.aData.airportcd;
}
}
//,
//"mDataProp": "airportcd"
},
{ "mDataProp": "airportname" },
{ "mDataProp": "cityname" },
{ "mDataProp": "statename" },
{ "mDataProp": "countryname" },
{ "mDataProp": "transportationtypecd" },
{ "mDataProp": "istauckprovided" },
{ "mDataProp": "iscitydefault" }
]


}
);
[/code]

Replies

  • kulkarni_ashkulkarni_ash Posts: 35Questions: 8Answers: 0
    I changed my code and added fnServerData as below, but it is not passing year to my controller, can i see what is the actual URL used

    Also will the control to aoColumns to build the data once the call is successfull?

    [code]
    $('#showByTourTable').dataTable({
    "bSort": false,
    "sPaginationType": "full_numbers" ,
    "bProcessing": true,
    "bDeferRender": true,
    "bDestroy": true,
    "sAjaxSource": 'getAirportAvaliableByTourYear',
    "fnServerParams": function ( aoData ) {
    aoData.push( { "year": $('#touryear').val(), "tourCd": $('#tourcode').val()} );
    },
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    alert('Ajax call ' + aoData);
    $.ajax( {

    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback,
    "error": alert('Error in getting data for AirportAvaliableByTourYear')
    }
    );
    },

    "aoColumns": [

    {
    "fnRender": function ( oObj ) {
    // return 'checkBox ' + oObj;
    var text = "";
    // return '';
    return text;
    }
    }
    );
    [/code]
  • kulkarni_ashkulkarni_ash Posts: 35Questions: 8Answers: 0
    edited February 2012
    So i got it working to call Controller, by changing [code] "data": aoData, to "data": { "year": $('#touryear').val(), "tourCd": $('#tourcode').val()} [/code]
    but it is not building the table,
    what is suppose to happen with success: fnCallback, where should the control go,
    i dont see the data reaching "aoColumns" at all

    what am i missing
  • kulkarni_ashkulkarni_ash Posts: 35Questions: 8Answers: 0
    This is how i got it working

    [code]
    var param = "?year="+$('#touryear').val()+"&tourCd="+$('#tourcode').val();
    $('#showByTourTable').dataTable({
    "bSort": false,
    "sPaginationType": "full_numbers" ,
    "bProcessing": true,
    "bDeferRender": true,
    "bDestroy": true,
    "sAjaxSource": 'getAirportAvaliableByTourYear'+param,
    "aoColumns": [

    {
    "fnRender": function ( oObj ) {
    ]
    }
    [/code]
This discussion has been closed.