【发布时间】:2012-01-16 17:03:11
【问题描述】:
我正在尝试使用 Spring 3 MVC 处理下面的 JSON:
var fields = {
"fields" : [ {
"groupId" : "mission",
"type" : "text",
"id" : "company_name",
"label" : "Nom de l'entreprise",
"size" : 0
}, {
"groupId" : "mission",
"type" : "text",
"id" : "theme",
"label" : "Thème",
"size" : 0
}, {
"groupId" : "mission",
"type" : "textarea",
"id" : "descriptive",
"label" : "Description",
"size" : 0
} ]
};
这是我发送它的方式:
$.ajax({
type: "POST",
url: "/GestaWeb/internshipConfiguration/proposal",
data: fields
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
通过 http 传递的参数(它是 Chrome 网络开发控制台的副本):
fields[0][groupId]:mission
fields[0][type]:text
fields[0][id]:company_name
fields[0][label]:Nom de l'entreprise
fields[0][size]:0
fields[1][groupId]:mission
fields[1][type]:text
fields[1][id]:theme
fields[1][label]:Thème
fields[1][size]:0
fields[2][groupId]:mission
fields[2][type]:textarea
fields[2][id]:descriptive
fields[2][label]:Description
fields[2][size]:0
还有我的 Spring 3 MVC 控制器:
package controller.internshipConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;
@Controller
@RequestMapping(value = "/internshipConfiguration")
public class ProposalModelConfigurationController {
@RequestMapping(value = "/proposal", method = RequestMethod.GET)
public ModelAndView form(ModelMap model) {
System.out.println("internship config view");
return new ModelAndView("internship/formModel");
}
@RequestMapping(value="/proposal", method = RequestMethod.POST)
public @ResponseBody String form(@RequestParam(value="fields") String fields) {
System.out.println("Fields rofl: " + fields);
return "ok";
}
}
当我执行 Ajax 时,我得到了 400。 如果我传递一个像这样的简单对象,它工作正常:{fields:“foo”}
【问题讨论】:
标签: ajax json spring-mvc