backbone ajax post json 请求 spring mvc 4.1.4 无法接收参数
使用backbone 模型的save方法,发送post请求
model.save(model.toJSON())
后端使用java 的springmvc框架 (4.1.4),
无法解析json注入对象,代码如下:
@RequestMapping(value = "/add", method = RequestMethod.POST, consumes = "application/json", produces = "application/json",headers ={"Accept=application/json"})
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public String add(@RequestBody Activity activity, HttpServletRequest request, HttpServletResponse response) {
上面加的注解是看了网上许多资料加的,但是还不是管用,还有返回415的错误。
另外还配置了jakson,也不好用,代码如下:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</list>
</property>
</bean>
求大神解答,在线等,急!!!
更新:在线等,急!!!
springMVC java json backbone.js
桐之丶卧所
9 years, 11 months ago
Answers
neKo.
answered 9 years, 11 months ago
自己回答,问题已解决。
spring mvc 不需要修改任何配置, 使用的backbone 版本为 1.0.0
修改backbone emulateJSON = true
//添加判断条件 !options.emulateJSON &&
// Ensure that we have the appropriate request data.
if (!options.emulateJSON && options.data == null && model && (method === "create" || method === "update" || method === "patch")) {
params.contentType = "application/json";
params.data = JSON.stringify(options.attrs || model.toJSON(options));
}
// For older servers, emulate JSON by encoding the request into an HTML-form.
if (options.emulateJSON) {
params.contentType = "application/x-www-form-urlencoded";
params.data = model.toJSON();//这一行做了修改
}
luck超人
answered 9 years, 11 months ago