jQuery Ajax return html AND json data

前端 未结 4 2198
终归单人心
终归单人心 2021-01-04 17:33

I\'m not sure if there is any way to do this or not, but this would solve so many of my problems if there is a simple solution to this.

What I need/want to be able t

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 17:54

    Trying to mix the retun value to contain presentation and data seems like a potential for confusion. Why not split it into two calls and fetch the data on success of the other?

    Something like:

     $.ajax({
      type: "POST",
      url: "inc/"+view_page+".php",
      data: "id="+encodeURIComponent(pageID),
      success: function(html) {
        $("body > .container").html(html);
        $.ajax({
          type: "POST",
          url: "inc/"+data_page+".php",
          data: "id="+encodeURIComponent(pageID),
          success: function(json) {
            $("title").html(json.PageTitle);
          }
        });
      });
    

提交回复
热议问题