I'm formulating an Ajax request using prototype in 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
And I have a servlet which tries to get the parameter That I put in the ajax method, but isn't finding any parameters in the request object. And if I try to get the attribute I've set it's listed as being null.
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));
}
}
Am I not instantiating the Ajax request properly or Am I making a mistake in getting the parameters?
EDIT
I've changed the javascript to clear any confusion about the name as follows:
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
The Java side is still not getting any parameters, I've added a loop that prints out all the parameters from request.getAttributeNames(); as follows:
Enumeration names = request.getAttributeNames();
System.out.println("Enumerating Attributes:");
while( names.hasMoreElements()){
System.out.println("[ELEMENT] "+ names.nextElement().toString());
}
and it does nothing aside from print out the line before the loop.
I've also tried putting a bigger array in the params and that doesnt work either, the loop still doesn't iterate.
I've run the Java end in debug mode and there are no attributes being sent. At least not as far as java can see.
I've also tried changing the paramaters to parameters: "step=1&garbage:'hello world'&foo='bar'" because the Prototype docs say you can do that no attributes server side. I've changed the mode to get and appended it to the url and there are no attributes...