A924110137
Chicken庆
2017-08-30 07:45

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

  • ajax

前台代码:

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条回答

为你推荐

换一换