2 a924110137 A924110137 于 2017.08.30 15:45 提问

Ajax后台向前台传json或者string类型的数据,前台收到的数据都是object

前台代码:

function pay(){
var map = new Map();
$.ajax({
type:"POST",//提交请求的方式
url:"/interface/servlet/paypay",//访问servlet的路径
dataType:"json",//没有这个,将把后台放会的json解析成字符串
data:$('#pay_submit').serialize(),//把内容序列化
async:true,//是否异步
error:function(data) {//请求出错
alert("出错");
},
success:function(data) {//获得返回值
console.log(data.allmap);
alert(data.BUSI_DATA.ORDER_FEE);

                        //var json =eval("("+data+")"); 
                          var bankForm = document.createElement("form");
                          bankForm.method = "post";
                          bankForm.action = "http://211.103.90.62:7061/payagent/PayUnifiedAccess";
                          bankForm.id = "IDataForm";
                          //var json = eval("("+map+")");
                          //for(var x in json){
                              var input = document.createElement("input");
                              input.id = "data";
                              input.name = "data";
                              input.type = "hidden";
                              //input.value ='{"PUB_INFO":{"BUSI_CODE":"1001","OS":"PC","VERIFY_CODE":"a902f90288e4fe8d6e2196499aa90f11b1efd98565f87ce14d9b39701e9d71794bde30a4269e69bd3caf3f754e5c6f9104c67abf207e869210979baf51ce7741f4831ee469e6c76c5828998b0f5d6febf3b95f7f9d9d23908beab2694c7279d52503498a6de5f2109ca68a43f89b3f7068892e29ab5bb34d739acb8fc3d71841"},"BUSI_DATA":{"ORDER_FEE":"1","GOODS_LIST":[{"TOTAL_MONEY":"1","GOODS_PRICE":"100","GOODS_NUM":"1","GOODS_NAME":"诺基亚","GOODS_ID":"0058812","GOODS_ITEM":"1"}],"PEER_TRADE_ID":"2017073011216092","ORDER_DESC":"陈小庆测试","MERCHANT_ID":"1517061200000251","ACCOUNT_CODE":"18322581301","VALID_TIME":"30"}}';//
                              input.value = data;
                              bankForm.appendChild(input);
                          //}
                          document.body.appendChild(bankForm);
                          var submitAction = document.getElementById("IDataForm");
                          submitAction.submit();
                    }
                });


         }
            后台代码:
                protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    log.debug("================");
     IData goodsInfo=new DataMap();
     IDataset goodsList=new DatasetList();
     DataMap pubInfo=new DataMap();
     DataMap busiDataMap=new DataMap();
     DataMap allMap=new DataMap();
     goodsInfo.put("GOODS_ITEM", "1");
     goodsInfo.put("GOODS_NAME", "诺基亚");
     goodsInfo.put("GOODS_PRICE", "100");
     goodsInfo.put("GOODS_NUM", "1");
     goodsInfo.put("TOTAL_MONEY", "1");
     goodsInfo.put("GOODS_ID", "0058812");
     goodsInfo.put("TOTAL_MONEY", "1");
     goodsList.add(goodsInfo);
     //goodsList.push(GOODS_LIST=goodsInfo);
     pubInfo.put("OS", "PC");
     pubInfo.put("BUSI_CODE","1001"); 
     busiDataMap.put("PEER_ORDER_ID","2017073011216092");  
     busiDataMap.put("ORDER_DESC","测试");
     busiDataMap.put("ORDER_FEE","1");
     busiDataMap.put("VALID_TIME","30");
     busiDataMap.put("ACCOUNT_CODE","18322581301");
     busiDataMap.put("MERCHANT_ID","1517061200000251");//商户号
     busiDataMap.put("GOODS_LIST",goodsList);//商户号
     allMap.put("PUB_INFO",pubInfo);
     allMap.put("BUSI_DATA",busiDataMap);
     log.debug(allMap);
    JSONObject json=JSONObject.fromObject(allMap);
    json.accumulate("data",allMap);
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/html");  
    PrintWriter out=resp.getWriter();
    //out.print(JSON.toJSONString(json));
    out.print(json.toString());
    out.flush();
    out.close();

}

    后台代码中map包含多个集合,这些参数能直接以json形势传到前台整体取出来吗?求大神指教啊

4个回答

oliver_105397
oliver_105397   2017.08.30 16:10

DataMap allMap=new DataMap();
这里为什么不用List>来封装呢

A924110137
A924110137 因为写接口的人给我提供的接口传参就是这个鸟样,哈哈
3 个月之前 回复
oliver_105397
oliver_105397 List<Map<Object, Object>>
3 个月之前 回复
JSXHMQS
JSXHMQS   2017.08.30 16:14

你在页面上打开一下你后台servlet的连接 页面就会显示你后台返回的数据形式了

A924110137
A924110137 后台是这样的 的确是json
3 个月之前 回复
qq_33727653
qq_33727653   2017.08.30 17:00

‘前台收到的数据都是object‘ 这个是对的呀 你在前台收到的data 不就是你后台的JSONObject json这个对象啊 你怎么放进去的再怎么取出来不就行了?

楼主是json.accumulate("data",allMap);这样放的,自然是data.data取了。楼主是哪里偏差了?还是我理解错了? 还有就是楼主前台的data是后台的json!!!楼主弄混了?

A924110137
A924110137 是啊 我试过了 data.data还是取出来的是object,我也纳闷呢
3 个月之前 回复
congge_1993
congge_1993   2017.08.30 17:31

看下response中返回的数据格式,应该是json格式的,data.data.key这种就可以取值

Csdn user default icon
上传中...
上传图片
插入图片