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

桐之丶卧所 10 years ago

写个filter,在spring mvc前面拦截一下, 看requst里的内容,看看有没有什么发现

夏洛特.德诺阿 answered 10 years ago

之前遇到过一个angular提交的请求后台php无法解析,主要是发送的和接收的类型不匹配

链接: angularjs向后台传参,后台收不到数据

只能帮到这里了,给楼主一个思路吧,顺便顶一下

neKo. answered 10 years ago

同求!!!在线等!!!

JackyP. answered 10 years 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 10 years ago

Your Answer