您的位置:首页 > Web前端 > JavaScript

88.使用JavaScript发送GET or Post 请求

2017-04-21 16:16 288 查看
1 首先点击事件 或者a标签连接 到js函数

<a class="btn_pay" href="javascript:account();">去结算</a>

2.然后写 function

//进入订单页面
function account() 
{
var url = "Order/enOrderViaCart";
var itemCartsJSON = '${itemCartsJson}';
var totalJSON = '${totalJson}';
var params = {itemCarts:itemCartsJSON, total:totalJSON};
var temp = document.createElement("form");
   temp.action = url;
   temp.method = "post";
   temp.style.display = "none";
   for (var x in params) {
       var opt = document.createElement("input");
       opt.name = x;
       opt.value = params[x];
       temp.appendChild(opt);
   }
   document.body.appendChild(temp);
   temp.submit();
   return temp;
}

3.然后进入action或者叫control,反正就是控制层

@RequestMapping(value = "/enOrderViaCart" )
public String  enOrderViaCart(HttpSession session, @RequestParam("itemCarts") String itemCartsJson, 
@RequestParam("total") String totalJson, Model model)

List<ItemCart> itemCarts = JSONUtil.getListObj(itemCartsJson, ItemCart.class);

Map<String, Object> total = JSONUtil.getMapObj(totalJson);

本人用的 alibaba的fastJSON,很好用推荐大家使用,这是我的工具类,感兴趣的可以直接copy,另外要下载三个jar包,网上可以下到,如果不想下载可以找我要 QQ 2032478859 

fastjson-1.1.37.jar

httpclient-4.2.5.jar

httpcore-4.4.4.jar

下面是我的工具类 JSONUtil 

package com.saiyou.util;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.TypeReference;

import org.apache.http.util.TextUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.util.List;

import java.util.Map;

/**

 * json工具类

 * 

 * @author Robin 2014年7月14日 上午11:23:21

 */

public class JSONUtil {

    protected static Logger jSONUtil = LoggerFactory.getLogger(JSONUtil.class);

    /**

     * 根据一个对象输出为json格式字符串

     * 

     * @param value

     * @return

     */

    public static String object2JSON(Object value) {

        String str = JSON.toJSONString(value);

        return str;

    }

    /**

     * 根据json字符串生成一个java对象

     * 

     * @param jsonText

     * @param clazz

     * @return

     */

    public static <T> T json2Object(String jsonText, Class<T> clazz) {

        return JSON.parseObject(jsonText, clazz);

    }

    /**

     * 对单个javabean进行解析

     * 

     * @param <T>

     * @param json 要解析的json字符串

     * @param cls

     * @return

     */

    public static <T> T getObj(String json, Class<T> cls) {

        T t = null;

        try {

            t = JSON.parseObject(json, cls);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return t;

    }

    /**

     * 对list类型进行解析

     * 

     * @param <T>

     * @param json

     * @param cls

     * @return

     */

    public static <T> List<T> getListObj(String json, Class<T> cls) {

        List<T> list = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                list = JSON.parseArray(json, cls);

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return list;

    }

    /**

     * 对MapString类型数据进行解析

     * 

     * @param json

     * @return

     */

    public static Map<String, String> getMapStr(String json) {

        Map<String, String> mapStr = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                mapStr = JSON.parseObject(json, new TypeReference<Map<String, String>>() {

                });

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return mapStr;

    }

    /**

     * 对MapInteger类型数据进行解析

     * 

     * @param json

     * @return

     */

    public static Map<Integer, Integer> getMapInt(String json) {

        Map<Integer, Integer> mapStr = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                mapStr = JSON.parseObject(json, new TypeReference<Map<Integer, Integer>>() {

                });

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return mapStr;

    }

    /**

     * 对MapObject类型数据进行解析

     * 

     * @param json

     * @return

     */

    public static Map<String, Object> getMapObj(String json) {

        Map<String, Object> mapStr = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                mapStr = JSON.parseObject(json, new TypeReference<Map<String, Object>>() {

                });

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return mapStr;

    }

    /**

     * 对list map obj类型进行解析

     * 

     * @param json

     * @return

     */

    public static List<Map<String, Object>> getListMap(String json) {

        List<Map<String, Object>> list = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                list = JSON.parseObject(json, new TypeReference<List<Map<String, Object>>>() {

                });

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return list;

    }

    /**

     * 对list map String类型进行解析

     * 

     * @param json

     * @return

     */

    public static List<Map<String, String>> getListMapStr(String json) {

        List<Map<String, String>> list = null;

        if (!TextUtils.isEmpty(json)) {

            try {

                list = JSON.parseObject(json, new TypeReference<List<Map<String, String>>>() {

                });

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return list;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: