【问题标题】:How to pass ajax into a spring controller如何将ajax传递给spring控制器
【发布时间】:2017-10-16 08:35:51
【问题描述】:

在 Ajax 页面中获取值

function GetInfoDivision()
{
     var data = $("#storeName").val();
    $.ajax({
        type : "POST",
        contentType : "application/json",   
        url : "hello",
        data : JSON.stringify(data),    
        dataType : 'json',              
        //timeout : 100000, 
        success : function(map) {
            console.log("SUCCESS: ", data);
            display(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });

但控制器页面获取空值...Ajax 数据值未传递给控制器​​

@RequestMapping(value="/hello", method = RequestMethod.POST)
        public String employeeLogin(ModelMap model, HttpServletRequest request) {
         String sname = request.getParameter("storeName");   
         System.out.println("s="+sname);    
            shopModel s = new shopModel();
              s.setStoreName(sname);

            //boolean result = employeeService.employeeLogin(employee);
              boolean result =false;
             if(result == true){

                    model.addAttribute("message", "Successfully logged in.");
             }
             else
             {
                model.addAttribute("message", "Username or password is wrong.");
            }
            return "redirect:index.jsp";
        }

【问题讨论】:

  • 你试过不对你的数据进行字符串化吗?
  • @Ivin Raj:你的“数据”只是一个字符串值,你不能只是 JSON.stringify 那样。要了解,您应该打开 Chrome F12 开发者工具下的“网络”选项卡以查看实际发送的内容。
  • Restcontroller 注解在哪里添加?
  • @HoàngLong 查看我更新的图片
  • @IvinRaj:您的图片显示的是 Source 选项卡,而不是 Network 选项卡。

标签: javascript java jquery ajax spring-mvc


【解决方案1】:

你应该在控制器函数的参数中使用@RequestBody注解。

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestBody.html

@Autowired
private HttpServletRequest request;

@RequestMapping(value="/hello", method = RequestMethod.POST)
public String employeeLogin(@RequestBody ModelMap model) {      

【讨论】:

  • 可能会从方法参数中删除HttpServletRequest request。我编辑了答案。如果你需要访问 Request 对象,你可以将它注入你的类中。
【解决方案2】:

如果 storeName 只是一个字符串,那么您可以使用 @RequestParam

@RequestMapping(value="/hello", method = RequestMethod.POST)
        public String employeeLogin(ModelMap model, HttpServletRequest request,
@RequestParam(value = "storeName", required = false) String storeName) {

String sname = storeName;
}

在 Ajax 调用中,您可以进行类似调用

url : "hello" + "?storeName=" + data

并在 Ajax 调用中删除以下属性

data : JSON.stringify(data), 

您的 Ajax 将如下所示:

function GetInfoDivision()
{
     var data = $("#storeName").val();
    $.ajax({
        type : "POST",
        contentType : "application/json",   
        url : "hello" + "?storeName=" + data,
        dataType : 'json',              
        //timeout : 100000, 
        success : function(map) {
            console.log("SUCCESS: ", data);
            display(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });

【讨论】:

  • 如何在 Ajax 数据中使用多个值@Amit K Bist
  • 它在单个值中工作很好,但我需要在 Ajax 数据中传递多个值@Amit K Bist
  • 这取决于你是否想以@RequestParam@RequestBody 的形式传递多个值,如果你想以@RequestParam 的形式传递给控制器​​,那么你可以执行"hello" + "?storeName=" + data + "&secondParam=" + secondParamValue 或您可以将 ajax 中的数据节点作为 data: ({storeName : data, secondParam: secondParamValue}) 传递,并且 url 在 ajax 调用中应该只有 url : "hello"。
  • 调用如下所示:var firstParamValue = $("#firstParam").val(); var secondParamValue = $("#secondParam").val(); $.ajax({ type : "POST", contentType : "application/json", url : "hello", data: ({firstParamName : firstParamValue, secondParamName: secondParamValue}), dataType : 'json', success : function(map) { console.log("SUCCESS: ", data); } });
  • 控制器就像public String employeeLogin(@RequestParam("firstParamName") String firstParamName, @RequestParam("secondParamName") String secondParamName, ModelMap model, HttpServletRequest request)
【解决方案3】:
@RequestMapping(value = "hello", method = RequestMethod.POST)
@ResponseBody
public String methodname(@RequestParam("data") String data) {
    ...
    return "";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多