从ajax到Spring MVC的400错误请求错误

时间:2015-02-03 11:38:41

标签: jquery ajax spring

Url不会向控制器发送jsp ajax调用。以下ajax调用函数不会转到控制器。它显示400 Bad Request错误。

if (updateList.length != 0) {    
    isoffhireUpdated = "Y";
    for (var i = 0; i < data.length; i++) {
        data[i].name = data[i]["name"];
        data[i].relationship = data[i]["relationship"];
        data[i].dateOfBirth = data[i]["dateOfBirth"];
        data[i].maritalStatus = data[i]["maritalStatus"];
        data[i].gender = data[i]["gender"];
        data[i].address = data[i]["address"];
    }
}

$.ajax({
    type: 'POST',
    cache: false,
    url: "dependencyDetailsSave",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        $('div.jGrowl').find('div.jGrowl-notification').parent().remove();
        $.jGrowl("Data was Successfully Saved", {
            sticky: false
        });
        $("#dependency_details").pqGrid("refreshDataAndView");
    }
});

这是我的控制器类功能。

@RequestMapping(value = "/dependencyDetailsSave", method = RequestMethod.POST)
@ResponseBody
public int dependencyDetailsSave(@RequestBody List<DependencyMasterDetails> obj, Model model,HttpSession session)throws IOException, Exception {
    int offhireupdateResult = 0; 
    offhireupdateResult = profileService.changeOffhireValueUpdate(obj);       
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我建议您验证从DependencyMasterDetails传递的JSON类和AJAX字段中的字段。

如果Front end(AJAX)和Back end(JAVA)之间的字段不匹配(名称),则大部分时间都会发生。

您从AJAX传递的所有字段必须出现在DependencyMasterDetails类中,且名称相同,除非您明确忽略它们(@JsonIgnore)。