0

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

TheSprinter
  • 1,523
  • 17
  • 30
Raphael Obadia
  • 333
  • 5
  • 14

1 Answers1

2

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:

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
TheSprinter
  • 1,523
  • 17
  • 30
  • Hello, i almost got it, but here i have done : i have : the get $.get("/index",{"query": que}, function .... and i catch up the parameter "que" with input val(). but here the issues, the get is trigged only once. how to trigged $.get again ? i tryed this but with no succes : $( "input" ) .keyup(function() { var value = $( this ).val(); $( "p" ).text( value ); }) .keyup(); – Raphael Obadia Feb 05 '19 at 12:04