【发布时间】:2016-07-19 05:58:18
【问题描述】:
我有以下 Spring 控制器映射:
@RequestMapping(value="/isSomethingHappening", method = RequestMethod.POST)
public @ResponseBody JsonResponse isSomethingHappening(HttpServletRequest httpRequest,@RequestParam("employeeId") String employeeId,
ModelMap model) throws IOException{
如果我按以下方式调用它,那么我会得到 400 响应。
var requestData = {"employeeId":XYZ.application.employeeId};
XYZ.network.fireAjaxRequestAsync("application/json", "forms/testing/isSomethingHappening", requestData, function(response, status, xhr){
但是,如果我按如下方式调用它,那么我会得到成功响应。
var requestData = {"employeeId":XYZ.application.employeeId};
XYZ.network.fireAjaxRequestAsync("application/x-www-form-urlencoded", "forms/testing/isSomethingHappening", requestData, function(response, status, xhr){
我想出了解决办法,但是当我的请求数据对象 var requestData = {"employeeId":XYZ.application.employeeId}; 保持不变并且我只是更改了内容类型时,我无法理解为什么第一个给我错误。 对我来说,application/json 看起来更适合内容类型,因为我的请求数据是 JSON 对象。
此外,我还有其他实例,其中我的控制器映射如下:
@RequestMapping(value = "/getOnFlyResults", method = RequestMethod.POST)
public @ResponseBody JsonResponse getOnFlyResults(HttpServletRequest httpRequest,
@RequestBody testingRequestVO testingRequestVO, ModelMap modelMap) throws IOException{
为了调用它,我发送如下请求:
var requestData = {"employeeId":XYZ.application.employeeId,
"fName":XYZ.application.fName,
"lName": XYZ.application.lName,
"telephoneNumber":telephoneNumber,
"testMode":XYZ.constant.onFly};
XYZ.network.fireAjaxRequestAsync("application/json", "forms/testing/startTest", JSON.stringify(requestData), function(response, status, xhr){
我不明白为什么我必须使用JSON.stringify(requestData) 对数据进行字符串化,如果我不这样做,我将得到 400。
一旦我字符串化然后它变成一个字符串然后我的内容类型应该是text/plain 但它适用于application/json
请注意,我知道代码修复,但我想了解这个概念。我已阅读 this 并没有详细解释我的概念和疑问。
【问题讨论】:
-
您是否已将 jackson 库配置为您的应用程序的依赖项?
-
使用 Maven?摇篮?
标签: java spring spring-mvc mime-types