TP中ajax請求如何使用模板渲染代替html代碼拼接

思路 index.html去ajax請求index控制器下的getdata()方法,在getdata()方法裏面把數據在cesuan_detail模板裏面渲染完,做爲結果返回給index.html裏。php

代碼html

index.htmlajax

var postData = {"xing" : xing, "ming": ming, "sex": sex,"birthday":birthday,"product":product};
      $.post("/index/getdata", postData, function(result){
          if (result.status == 1){
              $(".fitWxParent").show();
              $(".fitWx").html(result.data.htmlStr);
          }else{
              layui.layer.msg(result.msg, {icon:5});
          }
      }, "json");
複製代碼

IndexController.class.phpjson

public function getdata(){
        $xing = I("xing");
        $ming = I("ming");
        $sex = I("sex");
        $birthday = I("birthday");
        $product = I("product");
        $name_list = [1,2,3,4,5,6];
        $isFit = true;
        $this->assign('isFit',$isFit);
        $this->assign('name_list',$name_list);
        $cxdata = $this->fetch('Public/cesuan_detail');
        ajaxSuccess([
            'htmlStr' => $cxdata
        ]);
    }
複製代碼
相關文章
相關標籤/搜索