html中獲得session域中的id並使用jquery傳送ajax完成動態拼接a標籤連結地址
需求:html中獲得session域中的id並使用jquery傳送ajax完成動態拼接a標籤連結地址
思路:
第一步:在使用者註冊時將使用者的id存入session域中
第二步:在html得到session域中的id,通過ajax向後端傳送請求獲得id,並完成拼接。
後端程式碼為:request.getSession().setAttribute("patientID",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); } }
$('#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")
});
`
