将JSON数组传递给Spring MVC Controller [英] Passing in JSON array to spring MVC Controller

查看:94
本文介绍了将JSON数组传递给Spring MVC Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样将JSON数组传递到Spring MVC Controller中:

I am trying to pass a JSON array into spring MVC Controller like such:

 var myList = new Array(); 
data._children.forEach( function (d) { 
                        myList.push( {NAME: d.name, TYPE: d.TYPE, FDATE: d.FDATE } );
                    });

 $.post("/ListRequest", {myList: myList});

控制器的外观如下:

 @RequestMapping(value="/ListRequest", method = RequestMethod.POST)
    public void ListRequest(@RequestParam("myList") myList tempmyList )
    {
        System.out.println(tempmyList);
    }

myList类的定义如下:

The class myList defined as such:

public class MyList {
    private List<ListT> ListT;
    public List<ListT> getListT() {
        return ListT;
    }

    public void setListT(List<ListT> listT) {
        ListT = listT;
    }

}

ListT类:

public class ListT {
    private String NAME;
    private String TYPE;
    private Long FDATE; ...

我不断收到此错误: HTTP状态400-所需的myList参数"myList"不存在

I keep getting this error: HTTP Status 400 - Required myList parameter 'myList' is not present

也尝试了此请求:

$.ajax({
                type: "post",
                url: "ListRequest", //your valid url
                contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me)
                data: JSON.stringify(myList), //json object or array of json objects
                success: function(result) {
                    //do nothing
                },
                error: function(e){
                    alert('failure');
                }

但出现此错误:JBWEB000120:客户端发送的请求在语法上不正确.

but get this error: JBWEB000120: The request sent by the client was syntactically incorrect.

推荐答案

尝试将其添加到您的ajax调用中,它应该可以修复不受支持的响应:

try to add this to you ajax call it should fix the unsupported response :

headers : {
    'Accept' : 'application/json',
    'Content-Type' : 'application/json'
},

这是一个对我有用的ajax调用的完整示例:

this is a full example of ajax call that is working for me :

$.ajax({
            dataType : "json",
            url : this.baseurl + "/dataList",
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            data : JSON.stringify(params),
            type : 'POST',
            success : function(data) {
                self.displayResults(data);
            },
            error : function(jqXHR,textStatus,errorThrown ){
                showPopupError('Error','error : ' + textStatus, 'ok');
            }
        });

这篇关于将JSON数组传递给Spring MVC Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆