1

我正在尝试通过将 Ajax 回调到桌面版本来更新桌面网站的移动版本上的页面内容。该站点的移动版本位于一个专用子域中,并且该站点的桌面版本中的页面内容被打包为 JSON。Ajax 调用成功,因为我能够在控制台中注销 JSON 对象。但是,我无法隔离 JSON 对象的特定部分(在对象中标记为“内容”)。我写的函数不会在页面上放置任何东西(尽管它会在页面上放置其他 HTML,排除函数本身有问题),并尝试记录 JSON 对象的“内容”部分特别是在控制台中返回“未定义”。我用于整个过程的代码是这样的:

   function processJSON(url, id){
        $.ajax({
            url: url,
            data: {get_param : 'content'},
            success: function(response){
                $(id).html(response.content);                   
            },//ends success
            dataType: 'json',
        });//ends ajax
    };//ends processJSON

该函数的参数如下:“url”是 JSON 提要的 url(通常设置为变量),“id”是我试图在其中放置 HTML 的 div 的 id JSON 对象的“内容”部分。

JSON响应如下:

  {"status":"ok","page":{"id":9,"type":"page","slug":"scenes","url":"http:\/\/www.davidcharlesbrown.com\/scenes\/","status":"publish","title":"Scenes","title_plain":"Scenes","content":"<h3>I&#8217;ve always been wrapped up in the magic of the stage.<\/h3>\n<p>When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed me on stage, which was the kindest thing he could have done.<\/p>\n<p>Since then, I&#8217;ve been involved in over 100 productions in semi-professional, educational, community, and faith-based settings. I write, direct, design, and occasionally even act. I&#8217;ve also taught acting methods.<\/p>\n<p>I&#8217;m currently a member of the Autism Theatre Network, through the <a href=\"http:\/\/www.appliedtheatrecenter.org\/autismnetwork.html\" target=\"_blank\">Applied Theatre Center<\/a>. I&#8217;m involved with an agency that uses theatre to teach social pragamatics to children, adolescents, and young adults who are on the Autism spectrum.<\/p>\n<p>It&#8217;s a stage of a different sort.<\/p>\n","excerpt":"<p>I&#8217;ve always been wrapped up in the magic of the stage. When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed [...]<\/p>\n","date":"2013-04-10 14:08:27","modified":"2013-04-16 17:34:18","categories":[],"tags":[],"author":{"id":1,"slug":"truthscribe722gmail-com","name":"David Brown","first_name":"David","last_name":"Brown","nickname":"REDACTED","url":"","description":""},"comments":[],"attachments":[],"comment_count":0,"comment_status":"open","custom_fields":{}}}

谢谢!

4

1 回答 1

3

content不在对象的根,它在页面属性中。

$(id).html(response.page.content);  

{
    "status": "ok",
    "page": {
        "id": 9,
        "type": "page",
        "slug": "scenes",
        "url": "http://www.davidcharlesbrown.com/scenes/",
        "status": "publish",
        "title": "Scenes",
        "title_plain": "Scenes",
        "content": "<h3>I&#8217;ve always been wrapped up in the magic of the stage.</h3>\n<p>When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed me on stage, which was the kindest thing he could have done.</p>\n<p>Since then, I&#8217;ve been involved in over 100 productions in semi-professional, educational, community, and faith-based settings. I write, direct, design, and occasionally even act. I&#8217;ve also taught acting methods.</p>\n<p>I&#8217;m currently a member of the Autism Theatre Network, through the <a href=\"http://www.appliedtheatrecenter.org/autismnetwork.html\" target=\"_blank\">Applied Theatre Center</a>. I&#8217;m involved with an agency that uses theatre to teach social pragamatics to children, adolescents, and young adults who are on the Autism spectrum.</p>\n<p>It&#8217;s a stage of a different sort.</p>\n",
        "excerpt": "<p>I&#8217;ve always been wrapped up in the magic of the stage. When I was a junior in high school, I was cast in a chorus role of my first play. I froze as I was about to make my first entrance in front of an audience of several hundred people. The person behind me pushed [...]</p>\n",
        "date": "2013-04-10 14:08:27",
        "modified": "2013-04-16 17:34:18",
        "categories": [],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "truthscribe722gmail-com",
            "name": "David Brown",
            "first_name": "David",
            "last_name": "Brown",
            "nickname": "REDACTED",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "custom_fields": {}
    }
}
于 2013-07-13T19:54:52.283 回答