Search code examples
javaajaxservletsextjsextjs4

How to pass parameters to servlet using Ext.Ajax.request?


I have an extjs form from which I am trying to post parameter using Ext.Ajax.request to the servlet. Call is working and servlet is being called but for some reason parameter's value isn't being send. I'll post my code, can anyone tell me what I am doing wrong. Thanks in advance.

This is the call from ExtJS form:

buttons: [{

        text: 'Search',
        handler: function(){

                          var fName = Ext.getCmp("fName").getValue();

                          Ext.Ajax.request({

                               url : 'LookUPCustomer',

                               method: 'POST',
                               headers: { 'Content-Type': 'application/json'},                    
                               params : fName, // this value isn't being passed to servlet


                               success: function ( result, request ) {
                                    var resultData1 = JSON.parse(result.responseText);
                               },

                               failure: function ( result, request ) {
                                 resultData = JSON.parse(xmlhttp.responseText);
                               }        

                          });

               }]; 

and here is servlet code:

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {


         PrintWriter out = response.getWriter();

         // value of fName is null, not being passed from the form
         String fName = request.getParameter("fName");

         // does some processing....


    // print back to the form
         response.setContentType("application/json");

     out.println(jsArray);      
   }

Solution

  • The params parameter should be a JSON object with key, value pairs. Here's an example:

    params: {
               firstName: 'Jeff',
               lastName: 'Tester'
            }
    

    or to plug in your variable

    params: { fName: fName }