0

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 2

1

A spelling error jumped out at me: getQureyResults

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Thanks for your input! But here the problem is that the form is not at all getting submitted. So the controller isn't getting invoked at all.
Thanks for your input! But here the problem is that the form is not at all getting submitted. I am wondering if the onClick() event on the submit button is appropriate? For testing purpose I added action=getResult on the form tag like below <form action="getResult" target="result"> and tthis time the form gets submitted. But I dont want to use this way as I am using a asynchronous request. Any further suggestion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.