java - backbone ajax post json 请求 spring mvc 4.1.4 无法接收参数
迷茫
迷茫 2017-04-17 13:23:42
0
4
271
使用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>

求大神解答,在线等,急!!!

更新:在线等,急!!!

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
黄舟

Answer it yourself, the problem has been solved.

spring mvc does not need to modify any configuration, the backbone version used is 1.0.0

Modify 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();//这一行做了修改
            }
Ty80

Same request! ! ! Waiting online! ! !

小葫芦

I previously encountered a request submitted by angular that the background php could not parse, mainly because the sent and received types did not match

Link: angularjs transfers parameters to the background, but the background cannot receive data

I can only help here, give the poster an idea, and give me a thumbs up

Peter_Zhu

Write a filter and intercept it in front of spring mvc. Look at the content in requst to see if you find anything

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!