url特殊格式处理:

js中使用 encodeURIComponent() 编码对应的value

    $.ajax({
            type: "post",
            url: "/tb_area/insert?areaId=" + $('#areaId').val() + "&areaName=" + encodeURIComponent($('#areaName').val()),//js url 特殊字符处理
            contentType: "application/json",
            data: {},
            success: function (json) {
                alert("接口调用成功");
            }, error: function (xhr) {
                alert("错误提示: " + xhr.status + " " + xhr.statusText);
            }
        });

java中使用 URLEncoder.encode编码对应的value

     TbArea tbArea = tbAreaService.getTbArea(selectId);
        //java url 特殊字符
        String url = "http://127.0.0.1:8081/tb_area/insert?areaId=" + insertId + "&areaName=" + URLEncoder.encode(tbArea.getAreaName(), "UTF-8") + "1";
        log.info("url:" + url);
        String result = HttpXcUtil.httpRequest(url, "POST", "application/json", "", "", "");
        log.info("result:" + result);

 json 特殊格式处理:

使用JSON.stringify

        var param = {
            "areaId": "" + $('#areaId').val() + "",
            "areaName": "" + $('#areaName').val() + "",
        };
        $.ajax({
            type: "post",
            url: "/tb_area/insert2",
            contentType: "application/json",
            data: JSON.stringify(param),//json转string 会自动把换行符转成\n
            success: function (json) {
                alert("接口调用成功");
            },
            error: function (xhr) {
                alert("错误提示: " + xhr.status + " " + xhr.statusText);
            }
        })    

使用JSON.toJSONString

     TbArea tbArea = tbAreaService.getTbArea(selectId);
        String param = "{\n" +
                "  \"areaId\": \"" + insertId + "\",\n" +
                "  \"areaName\": " + JSON.toJSONString(tbArea.getAreaName()) + "\n" +//json转String 换行会被转成\n
                "}";
        log.info("param:" + param);
        String url = "http://127.0.0.1:8081/tb_area/insert2";
        log.info("url:" + url);
        String result = HttpXcUtil.httpRequest(url, "POST", "application/json", param, "", "");
        log.info("result:" + result);

源码:https://gitee.com/caoyeoo0/xc-springboot/tree/mybatis/

12-23 02:00