1

在我的 Spring 应用程序中,我将两个请求参数传递给我的业务逻辑..

$.ajax({
        url : "classes/addResult",
        type:'POST',
        data : {"names":names,"globalClassId":globalClassId}

    });

还有我的业务逻辑

@RequestMapping(value = "addResult", method = RequestMethod.POST)
public String addResult(ResultForm form,
        BindingResult result, Model model,
        @RequestParam("names") String[] names,
        @RequestParam("globalClassId") String globalClassId)
        throws Exception {
        -------------
        ------------
    return "";
}

但是控制器没有调用这个方法..为什么我的代码有问题..

4

2 回答 2

2
<script type="text/javascript">
   var names = new Array();
    $.ajax({
        url : "Result",
        type : 'POST',
        data : {
            "names" : JSON.stringify(names),//or names.join()
            "globalClassId" : globalClassId
        }});
</script>
于 2013-07-15T05:54:33.907 回答
1

如果您收到错误 400(错误请求)????????????

当您将数组数据(在您的情况下为 {names:names} )传递给 $.ajax() 方法时,它会在参数名称之后附加方括号 [] (意味着参数名称将是名称 [] //不是名称)

因此,您需要对代码进行一些更改

@RequestMapping(value = "addResult", method = RequestMethod.POST)
public String addResult(ResultForm form,
        BindingResult result, Model model,
        @RequestParam("names[]") String[] names, //replace names with names[]
        @RequestParam("globalClassId") String globalClassId)
        throws Exception {
        -------------
        ------------
    return "";
}

或者你可以使用

@RequestMapping(value = "addResult", method = RequestMethod.POST)
    public String addResult(ResultForm form,
            BindingResult result, Model model,
            @RequestParam("globalClassId") String globalClassId)
            throws Exception {
        String[] names = request.getParameterValues("names[]");//getting names array here
            -------------
            ------------
        return "";
    }
于 2013-07-13T05:20:07.353 回答