错误ajax无法将参数传递给Spring Controller

时间:2018-11-07 11:25:54

标签: ajax rest spring-mvc

var companyCode = [];

var data = {
        'companyCode': companyCode, //array this may be problem
        'actionUserGroup': usersession.userGroup,
        'formType': 'IN',
        'actionId': usersession.userId,
        'submittedDate': [submitted_s,submitted_e] //array this may be problem
};
console.log(data);

$.ajax({
    type : "POST",
    data :  JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    crossDomain: true,
    cache: false,
    url: appConfig.endPoint + 'search/myTask', 
    success: function(data){

    console.log(data);
    }
});

{此ajax面}

@RequestMapping(value = "/myTask", method = RequestMethod.POST)
public @ResponseBody JSONObject searchmyTask(Model model, HttpSession session,
        @RequestParam (value="companyCode") String[] companyCode,
        @RequestParam (value="actionUserGroup") String actionUserGroup,
        @RequestParam (value="formType") String formType,
        @RequestParam (value="actionId") String actionId,
        @RequestParam (value="submittedDate") String[] submittedDates
        ) throws Exception {

    /**
     * if "userGroup" = 'ADMIN'

{this Spring Controller}

http://localhost:9091/gcbg/search/myTask 400(不存在必需的String []参数'companyCode')jquery.min.js:2 POST

{这是一个错误}

请帮助我解决此问题。谢谢

1 个答案:

答案 0 :(得分:0)

我只是做了一些改动以使其正常工作,因此,如果您修改下一个选项,那么它应该对自己有用。

$.ajax({
    method : "POST",
    data :  data,
    crossDomain: true,
    cache: false,
    url: appConfig.endPoint + 'search/myTask', 
    success: function(data){
        console.log(data);
    }
});

在您的控制器中:

@RequestMapping(value = "/myTask", method = RequestMethod.POST)
public @ResponseBody JSONObject searchmyTask(Model model, HttpSession session,
    @RequestParam (value="companyCode[]") String[] companyCode,
    @RequestParam (value="actionUserGroup") String actionUserGroup,
    @RequestParam (value="formType") String formType,
    @RequestParam (value="actionId") String actionId,
    @RequestParam (value="submittedDate[]") String[] submittedDates
    ) throws Exception {

希望有帮助