前台Ajax发送数据给后台
前台发ajax请求给后台
前台代码
let data= [{receiveAdd:receiveAddVal, sendAdd:sendAddVal,distance:distance,goodsName:goodsName,editname:editname,weight:weight,editnotename:editnotename,dataTime:dataTime,receiveName:receiveName,receiveSex:receiveSex,receiveTel:receiveTel,sendName:sendName,sendSex:sendSex,sendTel:sendTel,price:price}] this.$ajax({ method: 'post', url: 'http://192.168.31.248:8080/sd_tech/PlaceOrder.do', dataType:'json', data:{ dataDetail: JSON.stringify(data) } }).then(response=>{ alert('post code done') this.$router.push({path:'/pay'}) }).catch(function(err){ console.log(err) });
也可以这样发送Ajax:
1 let data= [{receiveAdd:receiveAddVal, sendAdd:sendAddVal,distance:distance,goodsName:goodsName,editname:editname,weight:weight,editnotename:editnotename,dataTime:dataTime,receiveName:receiveName,receiveSex:receiveSex,receiveTel:receiveTel,sendName:sendName,sendSex:sendSex,sendTel:sendTel,price:price}] 2 this.$ajax({ 3 method: 'post', 4 url: 'http://192.168.31.248:8080/sd_tech/PlaceOrder.do', 5 dataType:'json', 6 data:{ 7 dataDetail: JSON.stringify(data) 8 }, 9 success:function (response) { 10 alert('post code done') 11 this.$router.push({path:'/pay'}) 12 }, 13 error:function (err) { 14 console.log(err) 15 } 16 })
后台代码:
@RequestMapping(value="PlaceOrder.do",produces="application/json;charset=utf-8") @ResponseBody public String PlaceOrder(@RequestBody Map<String,String> params, HttpServletRequest request,HttpServletResponse response, SdOrder sdOrder,String orderStartPoint)/* throws UnsupportedEncodingException*/{ //前端接收到的json串 /*dataDetail : [{"receiveAdd":"北京市通州区番茄俱乐部","sendAdd":"北京市通州区恺王国际","distance":9.463, "goodsName":"生活用品","editname":"11","weight":"<=5kg","editnotename":"11", "dataTime":["今天","上午","1点","20分"],"receiveName":"哈哈","receiveSex":"男", "receiveTel":"13633633666","sendName":"嘿嘿","sendSex":"男","sendTel":"15655655666","price":"28元"}]*/ System.out.println("总共获取到:"+params.size()+"个参数"); for(String key : params.keySet()){ System.out.println(key + " : " + params.get(key)); }