springboot前端向后端请求返回html语句
阅读原文时间:2023年07月08日阅读:2

后端接口代码

   @PostMapping("/service/confirmPay")
   @ResponseBody
   public GlobalResponse confirmPay(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        //调用方法
        String html = createHtml(payUrl, httpMap);
        return GlobalResponse.success(html);
   }

    public static String createHtml(String action, Map<String, String> paramMap) {
        StringBuilder html = new StringBuilder();
        html.append("<form id=\"submitForm\" name=\"submitForm\" action=\"").append(action).append("\" method=\"post\">\n");
        for (String key : paramMap.keySet()) {
            html.append("<input type=\"hidden\" name=\"" + key + "\"  value='" + paramMap.get(key) + "'>");
        }
        html.append("</form>\n");
        return html.toString();
    }

前端页面代码(js)

$.post(contextPath + '/service/confirmPay', param , function (res) {
            if(res.success){
                $("#payform").html(res.data);//获取后台返回的页面代码
            }else{
                layer.msg(res.alertMsg, {
                    time: 1600,
                    offset: ['40%']
                });
            }
        })