html中获得session域中的id并使用jquery发送ajax完成动态拼接a标签链接地址

需求:html中获得session域中的id并使用jquery发送ajax完成动态拼接a标签链接地址

思路:

第一步:在用户注册时将用户的id存入session域中

request.getSession().setAttribute("patientID",id);

第二步:在html得到session域中的id,通过ajax向后端发送请求获得id,并完成拼接。

后端代码为:

@RequestMapping("/getPatientId")
    @ResponseBody
    public void getPatientId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object patientID = request.getSession().getAttribute("patientID");
        System.out.println(patientID);
        if(patientID!=null){
            ObjectMapper objectMapper=new ObjectMapper();
            objectMapper.writeValue(response.getOutputStream(),patientID);
        }
    } 

前端代码为:

<button type="submit" id="btn_download"> 点击获得资源地址 </button>
<a href=""></a>

   $('#btn_download').click(function(){
        $.post("http://localhost:8080/FHIR/getPatientId",function(obj){
            var a =  document.links;
            //将后端返回的id拼接到请求路径中
            a[0].href="http://localhost:8080/FHIR/convert/down/"+obj;
            a[0].innerHTML="点击下载fhir文件";
                    },"json")

    });
posted @ 2022-05-05 17:07  李荣杰的博客  阅读(352)  评论(0)    收藏  举报