My approach to formulating an Ajax request using prototype involves the following method:
function updateServerStep(stepNumber){
alert("updating server step to " + stepNumber);
var params = {stepNumber:stepNumber};
alert(params.stepNumber);
var request = new Ajax.Request('UpdateStep', {
method:'Post',
parameters:params,
onSuccess: function(transport){
alert("Step changed to " + stepNumber);
},
onFailure: function(transport){
alert("Failed!");
}
});
}//updateServerStep
I am facing an issue with a servlet that is unable to retrieve the parameters I set in the Ajax method from the request object. The attribute I set is showing as null when I try to access it.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
Integer pageNumber = (Integer)request.getAttribute("stepNumber");
if (pageNumber != null){
System.out.println("page number was "+ pageNumber.intValue());
session.setAttribute("secPayStepNum", pageNumber);
} else {
System.out.println("page number was null");
session.setAttribute("secPayStepNum", new Integer(0));
}
}
I am unsure if I am instantiating the Ajax request correctly or if there is an error in retrieving the parameters.
EDIT
To clarify the name, I made changes to the JavaScript as shown below:
function updateServerStep(stepNumber){
alert("updating server step to " + stepNumber);
var params = {step:stepNumber};
alert(params["step"]);
var request = new Ajax.Request('UpdateStep', {
method:'Post',
parameters: {'step':"1"},
onSuccess: function(transport){
alert("Step changed to " + stepNumber);
},
onFailure: function(transport){
alert("Failed!");
}
});
}//updateServerStep
Despite the JavaScript adjustment, the Java side continues to not receive any parameters. I added a loop to print all parameters from request.getAttributeNames(); as shown below:
Enumeration names = request.getAttributeNames();
System.out.println("Enumerating Attributes:");
while( names.hasMoreElements()){
System.out.println("[ELEMENT] "+ names.nextElement().toString());
}
Unfortunately, the loop does not iterate and no attributes are being sent. I also tried passing a larger array in the params with no success. The Java end does not see any attributes.
I attempted changing the parameters to parameters: "step=1&garbage:'hello world'&foo='bar'" as per the Prototype docs, but it did not result in any attributes server-side. Changing the mode to 'GET' and appending it to the URL also did not send any attributes.