jQuery Ajax返回html AND json数据 [英] jQuery Ajax return html AND json data

查看:72
本文介绍了jQuery Ajax返回html AND json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否有任何方法可以做到这一点,但是如果有一个简单的解决方案,这可以解决我的很多问题.

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.

我需要/想要做的是在我的ajax请求成功后返回HTML和JSON.原因是,我想请求一个文件并返回该页面的所有内容,但是我还希望能够从json页面中返回指定的一组信息,因此我可以将其用于其他用途.

What I need/want to be able to do is return HTML and JSON in my success of ajax request. The reason being, I want to request a file and return all of that page, but I also want to be able to return a specified set of information from the page in json, so I can use it for other things.

这就是我现在正在做的事情:

This is what I'm doing now:

     $.ajax({
    type: "POST",
    url: "inc/"+page+".php",
    data: "id="+encodeURIComponent(pageID),
    success: function(html){

        $("body > .container").html(html);

      }
      });

这就是我想要做的:

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

在返回的页面上,我将指定标题的名称. (例如,如果是个人资料,我会返回用户名)

on the page that is being returned, I would specify what I want the title to be. (For instance, if it's a profile, I would return the user's name)

推荐答案

尝试混合retun值以包含表示形式和数据似乎有可能引起混淆.为什么不将其分成两个调用,并在另一个调用成功的情况下获取数据?

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?

类似的东西:

 $.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);
      }
    });
  });

这篇关于jQuery Ajax返回html AND json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆