gpt4 book ai didi

jquery - Spring MVC 3.0 带注释的 Controller 无法与 JQuery $.post() 一起使用

转载 作者:行者123 更新时间:2023-12-01 08:19:44 26 4
gpt4 key购买 nike

在 Spring MVC Controller 中,我试图访问 JQuery 简单帖子发送的名称。我收到错误在@RequestMapping* 中找不到@PathVariable [name]我哪里出错了?

JQuery 帖子

$.post("addName.htm",{ name: "John"});

Spring Controller

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@PathVariable String name) {

System.out.println("inside Setting value.... ");
System.out.println(name);
}

我收到错误

在@RequestMapping中找不到@PathVariable [name]

最佳答案

这不是 @PathVariable,您必须使用 @RequestParam 代替。试试这个:

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
System.out.println(name);
}

这就是区别:

  • 路径变量:http://yourhost/{name}/addName.html
  • 请求参数:http://yourhost/addName.html?name={name}

关于jquery - Spring MVC 3.0 带注释的 Controller 无法与 JQuery $.post() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com