I am attempting to add a security token to all my AJAX $.post calls.
I set up the security token to be automatically added using the following code
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
settings.data += "&sessionID="+v_sessionID;
}
});
and then code such as the following, will have the security token automatically appended
$.post("ajax/p_getOnePosition.jsp", {
positionNo: positionNo,
}, function(response2) {
The code works well, but not for functions with no parameters such as
$.post("ajax/p_clearFilters.jsp", {
}, function(response2) {
Any idea why this should be?
I can fix it by simply adding a dummy parameter dummy:1
to the code
$.post("ajax/p_clearFilters.jsp", {
dummy:1
}, function(response2) {
So I tried to do
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
var noParameters=(settings.data==null || settings.data==undefined || settings.data=='');
settings.data += (noParameters?"dummy=1":"") +"&sessionID="+v_sessionID;
}
});
but this did not mimic having the dummy=1
manually added, and the security token was not available in the request.
UPDATE
I realize in retrospect (after @Rory McCrossan's comment and @Louys Patrice Bessette's answer) that my question was incomplete. I should have also included in my question the JSP code that I use to retrieve the parameter.
<%
String csrfSessionID = SecurityIssues.StringToAlphaNumericStr(request.getParameter("sessionID"));
%>