0
0

Spring Bootで非同期でデータを取得する

Last updated at Posted at 2024-02-15

Controllerにメソッドを用意する

GETでも良いが、以下のようにPOSTを使ってFormで受け取ることができる

@Controller
@RequestMapping("/test")
public class TestController {

	@Autowired
	private TestService testService;

	@ResponseBody
	@RequestMapping("/getdata")
	public String getData(TestForm form) {
		return testService.getData(form.getCd(), form.getName());
	}
}

サーバで取得した値をHTMLに返す

    $.ajax({
        url: $(location).attr("host") + "/test/getdata",
        data: $("#TestForm").serialize(),
        type: "POST",
        dataType: "text"
    }).done(function(data) {
        $("#data").val(data);
    });
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0