0 0

jquery ajax前台通过json格式往后台传数据 后台对象接到的老是null30

前台的 静态json数据

var resContactInfos1 = [{ 

metadataId: '001',

orgCode:'001',

orgName: 'zhangsan', 

address: '20', 

            email: 'shanghai' 

        }, 

        { 

        metadataId: '002', 

        orgCode: '002', 

        orgName: 'beijing', 

        address: '21', 

        email: 'beijing' 

        } 

        ]; 

ajax 请求 

var data1={resContactInfo:resContactInfos1};

$.ajax({

          url: "rc/resourceCatalogManagerAction!saveResource.action", //后台处理的地址

          type: "POST",

          contentType: "application/json",

          data: JSON.stringify(data1),

          success:function(){

         alert("success");

          }

});

 后台action

private list<ResContactInfo>resContactInfo;

后台有个resContactInfo属性 它的get set方法也写了 

ResContactInfo这个类的属性为 metadataId 、orgCode等 同上面的json对象中的5个属性

 

问题  :我看到浏览器发的请求 感觉是没有问题的  但是后台显示接不到值  求解???

 

 

 

 


问题补充:我是参考这里做的  但是后台老是取不到值   不知道怎么回事
参考地址:http://cnn237111.blog.51cto.com/2359144/984466

问题补充:哎!还没解决呢。没办法了 只能换种方式处理了。
方法如下:后台直接传字符转
前台 request.getParameter("传的属性1");
前台 request.getParameter("传的属性2");
前台 request.getParameter("传的属性3");
对象用JSONObject转 list用JSONArray转 在做处理吧
不能用struts2的modelDriven 只是稍微麻烦一点 没法了
大家 谁找到原因或更好的方法联系我呀 小弟QQ: 1435648802
其实 类似楼下这位仁兄(QuarterLifeForJava )的博客 http://quarterlifeforjava.iteye.com/blog/2024336
2014年5月19日 16:13

8个答案 按时间排序 按投票排序

0 0

采纳的答案

可参考我的博客:http://quarterlifeforjava.iteye.com/blog/2024336
希望能帮到你

2014年5月20日 19:14
0 0

请问,后台你是如何接收的?

2014年5月20日 08:36
0 0

可能是你的格式错了:

如:你参考的代码中初始数据 后面的值是双引号 而你的是单引号

var persons = [{
                id: "001",
                name: "zhangsan",
                age: "20",
                gender: true,
                city: "shanghai"
            },
           {
                id: "002",
                name: "lisi",
                age: "21",
                gender: false,
                city: "beijing"
            }
           ];


var resContactInfos1 = [{

metadataId: '001',

orgCode:'001',

orgName: 'zhangsan',

address: '20',

            email: 'shanghai'

        },

        {

        metadataId: '002',

        orgCode: '002',

        orgName: 'beijing',

        address: '21',

        email: 'beijing'

        }

        ];

2014年5月20日 02:40
0 0

在ajax中添加dataType: "json"估计就可以了。

2014年5月19日 20:19
0 0

var data1={resContactInfo:resContactInfos1};
改为var data1={"resContactInfo":resContactInfos1};

2014年5月19日 18:29
0 0

$.ajax({

          url: "rc/resourceCatalogManagerAction!saveResource.action", //后台处理的地址

          type: "POST",

          contentType: "application/x-www-form-urlencod",

          data: JSON.stringify(data1),

          success:function(){

         alert("success");

          }

});


把contentType改一下就可以了

2014年5月19日 17:10
0 0


能把请求头信息截图看一下么?

我怀疑是
var data1={resContactInfo:resContactInfos1};
这一句多余了

2014年5月19日 16:52
0 0

好像ajax请求少个datatype吧

var markers = [{ "position": "128.3657142857143", "markerPosition": "7" },
               { "position": "235.1944023323615", "markerPosition": "19" },
               { "position": "42.5978231292517", "markerPosition": "-3" }];

$.ajax({
    type: "POST",
    url: "/webservices/PodcastService.asmx/CreateMarkers",
    data: JSON.stringify({ Markers: markers }),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg) {
        alert(errMsg);
    }
});

2014年5月19日 16:42

相关推荐

Global site tag (gtag.js) - Google Analytics