JQuery / Spring:将json对象发布到spring控制器不起作用

时间:2022-09-22 20:37:30

This is my spring controller

这是我的弹簧控制器

@RequestMapping(value = "/app/{appId}/save.html",method=RequestMethod.POST)
public @ResponseBody String myFunction 
     (@PathVariable("appId") String id, @RequestBody Map<String, String> data1) {
                return "hello";
   }

This is my ajax request

这是我的ajax请求

$.ajax({
                    type : 'POST',
                    url : '/app/${param.appID}/save.html',
                    data : JSON.stringify(myJsonData), 
                    dataType : "json",

                    success : function(data, textStatus,
                            xhr) {
                        var response = xhr.responseText;
                        if (response !== "hello") {
                            alert('sad');
                        } else {
                            alert('happy');
                        }
                    }
                });

The problem is , this request never reaches the controller and gets stuck . If I remove data and data1, things work fine. Can some help me with what am I doing wrong.

问题是,这个请求永远不会到达控制器并被卡住。如果我删除数据和数据1,一切正常。有些人可以帮助我解决我的错误。

Update: Here's the error as shown by firebug

更新:这是firebug所显示的错误

415 Unsupported Media Type : The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ()

415不支持的媒体类型:服务器拒绝此请求,因为请求实体所采用的方法所请求的资源不支持该格式()

Here is my json object

这是我的json对象

myJsonData= {
                        "data1" : $("input[name=key1]:checked").val(),
                        "data2" : $("input[name=key2]:checked").val(),
                        "data3" : $("input[name=key3]:checked").val()
                };

2 个解决方案

#1


0  

JSON.stringify is not necessary.

JSON.stringify不是必需的。

data : myJsonData,

is enough.

#2


0  

Try explicitly setting consumes="application/json" in @RequestMapping

尝试在@RequestMapping中显式设置consumemes =“application / json”

#1


0  

JSON.stringify is not necessary.

JSON.stringify不是必需的。

data : myJsonData,

is enough.

#2


0  

Try explicitly setting consumes="application/json" in @RequestMapping

尝试在@RequestMapping中显式设置consumemes =“application / json”