Raphael Obadia
Raphael Obadia

Reputation: 445

Spring Boot application with Jquery ajax

i would have need some tips/help here. I have nearly 0 experience with jquery but i might need it.

in my controller i have this :

@RestController
public class mainController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
    String res = drive.checkFile("cwg");

    return res;
}

for now, i succeeded to display the res in my view with jquery get. But i need to go further.

in my view (index.html), i need to pass the parameter with (jquery method?) a form and display the res.

like :

    @RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
    String res = drive.checkFile("***HERE PARAMETER***");

    return res;
}

I probably needed a POST ans GET method. but i have no idea how to achieve that. How to pars parameters to a controller method via html.

thx very much

Upvotes: 0

Views: 4277

Answers (1)

TheSprinter
TheSprinter

Reputation: 1528

In this case, you need to modify your method as below

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(@RequestParam String inputParameter) throws IOException, GeneralSecurityException {
    DriveQuickstart drive = new DriveQuickstart("c:/temp/credentials.json");
    String res = drive.checkFile(inputParameter);
    return res;
}

The section modified is Instead of Model model we used @RequestParam String inputParameter as an argument.

and from JQuery call this GET method and pass param as QueryString


Update: Your JQuery Method should similar to this:

$("input").keyup(function(){

  $.ajax({
    url: "/index",
    type: "get", //send it through get method
    data: { 
      inputParameter: value , // your get parameter(s)
      inputParameter2: value2, 
      inputParameter3: value3
    },
    success: function(response) {
      //Do Something on successful Ajax call
    },
    error: function(xhr) {
      //Do Something to handle error
  }
  });


});

refer to the below links:

Upvotes: 2

Related Questions