How to handle Ajax request in ATG form handler

When we want to handle a Ajax request in ATG

Step-by-step guide

On the form handler.
  1. Create a new handle method.
  2. Write the required logic inside the method
  3. Don't use checkFormRedirect(); create a json object instead and write it to the response.
    JSONObject responseJson = new JSONObject();
    responseJson.put("ShoppingCartItemCount", getOrder().getCommerceItemCount());
    pResponse.setContentType(APPLICATION_JSON);
    PrintWriter out = pResponse.getWriter();
    out.print(responseJson.toString());
    out.flush();
  4. The handle method should return 'false'.

On the jsp/javascript
  1. Create a usual dsp form with the submit method pointing to our handle method declared in form handler.
  2. Use javascript to do a ajax submit of the form.
  3. The response will be coming as json. Handle the response in javascript.

No comments:

Post a Comment