skip to Main Content

I am working on a simple project where I need to pass some form parameters like database name and a query to the Spring Controller. The Controller passes the query to the corresponding service class(corresponding to the dbname) and return the resultset as a java.util.List.
I am using Jquery/ajax to pass the values to the Controller but the values are not reaching till the Controller.
Below is my code.

JQuery/ajax

'''
    function ajaxAsyncRequest()
    {
    //Creating a new XMLHttpRequest object
    var xmlhttp;
    var dbname = document.getElementById("dbradio").value;
    var query = document.getElementById("myTextBox").value;
    alert(dbname)
    alert(query)

    $.ajax({
        type: "GET",
        url: "/getResult",
        data: "dbradio="+dbname+"&myTextBox="+query,

        success: function(response)
        {
            $('#resultList').html(response);
        },
        error: function(e)
        {
            alert('Error: ' + e);
        }
      });

    }

'''

JSP:

'''


    <form action="" target="result">

      <input type="radio" id="dbradio" value="mysql"> MySQL
      <input type="radio" id="dbradio" value="redshift"> RedShift

      <textarea id="myTextBox" cols="50" rows="10" style="background-color:#FCF5D8;color:#AD8C08;"> 
      </textarea>


       <p><input type="submit" value="Submit" onclick='ajaxAsyncRequest()'/></p>
       </form>

'''

Controller

'''



        @RequestMapping(value="/getResult", params = { "dbradio", "myTextBox" }, method = 
             RequestMethod.GET)

        public List<Map<String,Object>> getResult(@RequestParam("dbradio") String dbname, 
         @RequestParam("myTextBox") String query, HttpServletRequest req, HttpServletResponse res) {

        System.out.println("In Controller");
        System.out.println(dbname);
        System.out.println(query);
        List<Map<String,Object>> queryResult = service.getQureyResults(query);

        ModelAndView mv= new ModelAndView();
        mv.setViewName("index");    
        mv.addObject("result", queryResult);
        return queryResult;
          }

    '''

POM.xml entry

'''

    <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.2.4</version>
    </dependency>

    <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
    </dependency>

'''

Any help would be appreciated! Thanks

2

Answers


  1. Try passing data using JSON format

    $.ajax({
      type: "GET",
      url: "/getResult",
      data: {
        dbradio: dbname,
        myTextBox: query
      },
      success: function(response) {
        $('#resultList').html(response);
      },
      error: function(e) {
        alert('Error: ' + e);
      }
    });
    

    Hope this will solve your problem

    Login or Signup to reply.
  2. A spelling error jumped out at me: getQureyResults

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search