Most of my AJAX calls just work with the daya attribute in jQuery to pass singular values. But sometimes I need to pass a structure in JSON to the server for further processing. I know how to, I know it works. But it seems to work only 90% of the time.
The setup is Railo 4.2 and IIS, both have most of their settings still as default. I'm also unable to reproduce this locally, where I have Coldfusion server instead of Railo. I'm also working in the Coldbox framework.
The jQuery code that sends the JSON data is as follows:
var oJSON = {'param1': param1, 'param2': param2, 'param3': param3};
$.ajax({
type: "POST",
url: uAjax + "?action=someurlparameter",
data: JSON.stringify(oJson),
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function(oData)
{
// do something
}
});
The Coldfusion CFC that handles the incoming data does the following:
<cffunction name="cashup" returntype="struct" output="false" hint="handle json data">
<cfargument name="event">
<cfargument name="rc">
<cfargument name="prc">
<!--- get json data --->
<cfset LOCAL.oJson = deserializeJSON(toString(getHttpRequestData().content)) />
<cfreturn someService.someProcessing(LOCAL.oJson) />
</cffunction>
This seems pretty straight forward code to me. But for some reason 10% of the time these calls fail because the request body is empty. And I have no clue why. It can't be the data that is being sent, because clicking the button again, without refreshing the page, and thus retrying the ajax call, most of the time results in a good call, on that comes through WITH a filled request body.
edit 1:
sample data in json:
"MoneyLocation": {
"MoneyLocationPaymentTypeCountList": [
{
"Amount": 175.28,
"Description": "CASH ",
"IdPaymentType": 2,
"ImageLocation": ""
},
{
"Amount": 0,
"Description": "Generieke bon ",
"IdPaymentType": 62,
"ImageLocation": ""
}
],
"IdTill": -1,
"IdMoneyLocation": 35
}