I have the following piece of code to send serializable data on ajax call to servlet
$.ajax({
type: "post",
url: registersubmit.RegisterServlet.json,
dataType: "json",
data:$('#registrationForm').serialize(),
success: function(msg) {
// alert(msg.data);
alert('success'+msg.message2);
},
error: function (xhr, ajaxOptions, thrownError){
alert('HAI');
alert('BYE');
}
});
I have a form with the id registrationForm, and while I try to send the serializable data using the statement data:$(‘#registrationForm’).serialize(), Im getting the response “servlet temporarily moved with status code 302”.
Note:If I remove the statement data:$(‘#registrationForm’).serialize(), then Im getting the normal ajax response with status code 200.
May I know how we can send the serializable data on ajax call.
Thanks,
Balaji.
The 302 just means that the servlet has invoked
response.sendRedirect(newURL). Perhaps your servlet is doing that for some reason because it has successfully processed the request?When retrieving a redirected response in
$.ajax(), the browser won’t automatically change the main window. If you indeed actually need to change the main window to the given URL, then you’d need to let the servlet return some specific response containing the desired target URL and then let the JavaScript perform the redirect usingwindow.location=newURLwhereinnewURLis been extracted from the response.By the way, your term “serializable data” sounds very misplaced in the context. The
$.serialize()merely collects all input values of the form in a JSON object so that it can be sent as request parameters of$.ajax. In Java context, “serializable data” has an entirely different meaning.