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


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

    一、配置类配置地址。

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

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

    1、service层写方法。

    String apiDetail();

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

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

    3、再return回这个地址。 

    1. @Override
    2. public String apiDetail() {
    3. return apiDetail;
    4. }

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

    1. @RequestMapping(value = "/apiDetail",method = RequestMethod.POST)
    2. public ResultInfoDTO apiDetail(){
    3. ResultInfoDTO infoDTO = new ResultInfoDTO<>();
    4. try{
    5. String flag = lrmLawyerAbroadService.apiDetail();
    6. infoDTO.setData(flag);
    7. infoDTO.setReturnValue(true);
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. throw new BussinessException(Constant.FAIL, "异常");
    11. }
    12. return infoDTO;
    13. }

    四、设置事件。

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

    1. {
    2. name: 'lawyerName',
    3. index: 'lawyerName',
    4. sortable: false,
    5. formatter: function (cellvalue, options, rowObject) {
    6. return " + rowObject.lawyerTerritoryInfoId + "')>"+cellvalue+"";
    7. }
    8. },

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

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

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

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

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

    2、js中初始化方法。

    1. $(function () {
    2. if (parentParam.lhrTeamBaseextId) {
    3. detaiLawyerTerritory(parentParam.lhrTeamBaseextId);
    4. }
    5. })

    3、用Ajax实现方法请求。

    1. function detaiLawyerTerritory(lhrTeamBaseextId) {
    2. $.ajax({
    3. url: "调用的接口路径",
    4. type: 'post',
    5. async: false,
    6. success: function (data) {
    7. //debugger
    8. if (data && data.returnValue) {
    9. if (data.data) {
    10. var urlStr = data.data + "?lhrTeamBaseextId=" + lhrTeamBaseextId;
    11. layerMsg.iframeMSgSusView("律师详细信息", urlStr, "setBaseInfo", "", "98%", "98%", "关闭");
    12. }
    13. } else {
    14. layer.msg(data.msg, {
    15. time: 1500,
    16. icon: 2
    17. },
    18. function () {
    19. });
    20. }
    21. },
    22. error: function () {
    23. layer.msg(message.sysError, {time: 2000, icon: 2});
    24. }
    25. })
    26. }

     如此,便能实现跨域请求api接口的功能了。

  • 相关阅读:
    【环境搭建】linux docker安装nexus3
    Harris图像角点检测
    网络安全等级保护测评师定义以及主要工作任务是什么?
    springboot+vue+elementUI企业客户信息反馈平台#毕业设计
    git cherry pick
    JAVA基础总结【面试】
    MySQL数据库入门到精通8--进阶篇( MySQL管理)
    【Ubuntu20.04】安装XRDP远程桌面服务
    AI低代码维格云相册视图怎么用?
    SpringBoot+EasyExcel导入导出【加水印】
  • 原文地址:https://blog.csdn.net/m0_64818669/article/details/126585265