【问题标题】:Send javascript variables to spring controller将javascript变量发送到spring控制器
【发布时间】:2017-08-21 07:05:14
【问题描述】:

我正在尝试使用 spring mvc 开发简单的应用程序,我需要将 javascript 参数传递给 spring 控制器。我尝试了几种方法,但都没有奏效。以下是我的 javascript 和 spring 控制器。请帮我解决这个问题。

Java 脚本

function searchViaAjax(id) {
    alert(id);
    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "search/api/getSearchResult",
        data : JSON.stringify(id),
        dataType : 'json',
        timeout : 100000,
        success : function(id) {
            console.log("SUCCESS: ", id);
            display(id);
            alert(response);   
        },
        error : function(e) {
            console.log("ERROR: ", e);
            display(e);
        },
        done : function(e) {
            console.log("DONE");
        }
    });
}

AjaxController.java

@Controller
public class AjaxController {
    @ResponseBody
    @RequestMapping(value = "/search/api/getSearchResult")

    public String getSearchResultViaAjax(@RequestParam(value = "id") int id) {
        System.out.println("come to ajax"+ id);
        return "hello";

    }
}

【问题讨论】:

  • data : JSON.stringify({ id: id }),
  • 我试过这种方式。但对我不起作用

标签: javascript java spring spring-mvc


【解决方案1】:

当您将 json 作为 requestbody 传递时,上述调用适用。要发送请求参数,您必须如下:

使用以下 ajax 调用:

function searchViaAjax(id) {
var tempId = id;
$.ajax({
    type : "POST",
    url : "/search/api/getSearchResult",
    data : {id:tempId},
    timeout : 100000,
    success : function(id) {
        console.log("SUCCESS: ", id);
        display(id);
        alert(response);   
    },
    error : function(e) {
        console.log("ERROR: ", e);
        display(e);
    },
    done : function(e) {
        console.log("DONE");
    }
});
}

您也可以使用以下 get 方法实现此目的:

 function searchViaAjax(id) { 
 $.ajax({ 
 type : "GET", 
 url : "/search/api/getSearchResult/"+id, 
 timeout : 100000, 
 success : function(id) { 
 console.log("SUCCESS: ", id); 
 display(id); 
 alert(response); 
}, 
error : function(e) { 
console.log("ERROR: ", e); 
display(e); 
}, 
done : function(e) { 
console.log("DONE"); 
} 
}); 
} 

@Controller 
public class AjaxController { 

@ResponseBody 
@RequestMapping(value = "/search/api/getSearchResult/{id}") 
public String getSearchResultViaAjax(@PathVariable(value = "id") Integer id) 
{ 
 return String.valueOf(id); 
} 
}

【讨论】:

  • 感谢您的评论。但这对我不起作用。
  • 将 url 更改为 url : "/search/api/getSearchResult",
  • 它返回 hello 作为响应而不是 id,因为您返回了“hello”字符串
  • '/' in start 应该添加,因为它将此路径附加到应用程序的上下文路径
  • 我已经检查过了,但它对我不起作用。这是一个非常简单的问题,但我不知道为什么这不起作用。
【解决方案2】:

这是您从 ajax 向控制器发出的发布请求。 您的弹簧控制器上缺少“methodType=RequestMethod.POST”。

@RequestMapping(value = "/search/api/getSearchResult") 替换为

@RequestMapping(value = "/search/api/getSearchResult", methodType=RequestMethod.POST)

【讨论】:

    【解决方案3】:

    当您使用JSON.stringify 时,您实际上是在向您的spring 控制器的方法发送一个JSON 对象。您所要做的就是像这样将您的 json 对象包装为 java 对象。

    public class UserId {
    
        private int id;
    
        // setters and getters
    
    }
    

    在你的控制器方法中使用@RequestBody 将你的JSON 映射到你的UserId 类,就像这样

    @ResponseBody
    @RequestMapping(value = "/search/api/getSearchResult", method = RequestMethod.POST)
    public String getSearchResultViaAjax(@RequestBody UserId user) {
        System.out.println("come to ajax" + user.getId());
        return "hello";
    }
    

    PS:尚未测试,但应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多