使用jQuery AJAX春季启动应用程序

问题描述 投票:-1回答:1

我会需要一些技巧/在这里帮助。我有jQuery的几乎为0的经验,但我可能需要它。

在我的控制器,我有这样的:

@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;
}

现在,我成功地在我与jQuery的get视图中显示的资源。但我需要走得更远。

在我的视图(的index.html),我需要通过使用参数(jquery的方法?)的格式,并显示所述RES。

喜欢 :

    @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;
}

我可能需要一个POST和GET方法。但我不知道如何来实现这一目标。如何通过HTML参数传递到控制器的方法。

THX非常

jquery ajax spring-boot
1个回答
0
投票

在这种情况下,你需要如下修改你的方法

@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;

}

修改的部分是相反的,我们用Model model作为参数@RequestParam String inputParameter的。

和从JQuery的调用此GET方法并传递PARAM作为查询字符串


更新:您JQuery的方法应该与此类似:

$("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
  }
  });


});

请参考以下链接:

© www.soinside.com 2019 - 2024. All rights reserved.