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"));
%>
2
Answers
After working through @Rory McCrossan's comment and @Louys Patrice Bessette's answer - thank you very much - I see that when beforeSend adds to an empty parameter list, then the SESSIONID is sent as part of the Request Payload, and when beforeSend adds to pre-existing parameter list, then SESSIONID is sent as part of the Form Data. This makes a difference in retrieving the parameters.
For data as part of the Form Data then it can be retrieved by the following:
For data as part of the request Payload it can be retrieved by the following: See @Fizer Khan's answer in Getting request payload from POST request in Java servlet
These two code snippets are mutually exclusive. The first code cannot retrieve from the Request Payload, and the second code cannot retrieve from the Form Data
Note that probably a better answer would force the beforeSend to send the SessionID in the Form Data in all cases, but this is a good workaround for now!
You have three
data
cases:Try that
beforeSend
function:The below snippet always ends up in an error because the request url is https://example.com. 😉
Also on CodePen