un8.31:用jQuery实现调用不同项目api接口的功能。

在开发过程中,经常会遇到好几个项目一起进行的情况,那么如果说是要调用另一个项目的接口,该如何实现呢?小编今天就和大家分享一下。

一、配置类配置地址。

apiDetail:
  url: http://另一个项目中api的接口路径

二、完成基本的service层以及impl层。

1、service层写方法。

String apiDetail();

2、impl中先将地址传入。。

@Value("${apiDetail.url}")
    private String apiDetail;

3、再return回这个地址。

@Override
    public String apiDetail() {
        return apiDetail;
    }

三、controller中写接口,以后前端调用。

@RequestMapping(value = "/apiDetail",method = RequestMethod.POST)
    public ResultInfoDTO<String> apiDetail(){
        ResultInfoDTO<String> infoDTO = new ResultInfoDTO<>();
        try{
            String flag = lrmLawyerAbroadService.apiDetail();
            infoDTO.setData(flag);
            infoDTO.setReturnValue(true);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException(Constant.FAIL, "异常");
        }
        return infoDTO;
    }

四、设置事件。

1、在js中的字段中设置一个事件,将参数传入。

{
                name: lawyerName,
                index: lawyerName,
                sortable: false,
                formatter: function (cellvalue, options, rowObject) {
                    return "<span class=grid-opt onclick=detaiLawyerTerritory(" + rowObject.lawyerTerritoryInfoId + ")>"+cellvalue+"</span>";
                }
            },

2、实现此事件,并跳转到另一个页面。

function detaiLawyerTerritory(lhrTeamBaseextId) {
    window.location.href = /lrm/html/lawyerabroad/lawyerInfoDetail.html?lhrTeamBaseextId=+lhrTeamBaseextId;

五、跳转到另一个页面后实现此功能。

1、html中加载api地址,设置弹窗类型。

<div class="content-box" id="lhrTeamBaseextId">
    <iframe src="接口的api地址" width="100%" height="100%" frameborder="0" name="iframea"></iframe>
  </div>

2、js中初始化方法。

$(function () {
    if (parentParam.lhrTeamBaseextId) {
        detaiLawyerTerritory(parentParam.lhrTeamBaseextId);
    }
})

3、用Ajax实现方法请求。

function detaiLawyerTerritory(lhrTeamBaseextId) {
    $.ajax({
        url: "调用的接口路径",
        type: post,
        async: false,
        success: function (data) {
            //debugger
            if (data && data.returnValue) {
                if (data.data) {
                    var urlStr = data.data + "?lhrTeamBaseextId=" + lhrTeamBaseextId;
                    layerMsg.iframeMSgSusView("律师详细信息", urlStr, "setBaseInfo", "", "98%", "98%", "关闭");
                }
            } else {

                layer.msg(data.msg, {
                        time: 1500,
                        icon: 2
                    },
                    function () {
                    });
            }
        },
        error: function () {
            layer.msg(message.sysError, {time: 2000, icon: 2});
        }
    })
}

如此,便能实现调用不同项目的api接口。

经验分享 程序员 微信小程序 职场和发展