首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >未显示来自json的图像

未显示来自json的图像
EN

Stack Overflow用户
提问于 2013-06-27 18:44:09
回答 2查看 926关注 0票数 0

我无法使用jquery和ajax显示以下json中的图像(附件、=>图像和完整的=> url):

代码语言:javascript
运行
复制
{
 "status": "ok",
 "post": {
  "id": 117,
"url": "https:\/\/domain.com\/classifieds\/%d0%bf%d1%80%d0%be%d0%b4%d0%b0%d0%bc-%d0%b0%d0%b2%d1%82%d0%be%d0%bc%d0%be%d0%b1%d0%b8%d0%bb%d1%8c\/",
"title": "\u041f\u0440\u043e\u0434\u0430\u043c \u0430\u0432\u0442\u043e\u043c\u043e\u0431\u0438\u043b\u044c",
"content": "<p>\u0426\u0432\u0435\u0442 \u043a\u0443\u0437\u043e\u0432\u0430: ",
"date": "2013-06-26 10:30:06",
"modified": "2013-06-26 10:33:06",
"tags": [],
"attachments": [
  {
    "images": {
      "full": {
        "url": "https:\/\/domain.com\/wp-content\/uploads\/2013\/06\/Vw-Golf.jpg",
        "width": 650,
        "height": 433
      },
      "large": {
        "url": "https:\/\/domain.com\/wp-content\/uploads\/2013\/06\/Vw-Golf.jpg",
        "width": 650,
        "height": 433
      },
      "small": {
        "url": "https:\/\/domain.com\/wp-content\/uploads\/2013\/06\/Vw-Golf-120x79.jpg",
        "width": 120,
        "height": 79
      },
      "custom-size": {
        "url": "https:\/\/domain.com\/wp-content\/uploads\/2013\/06\/Vw-Golf-650x200.jpg",
        "width": 650,
        "height": 200
      }
    }
  }
]
 }
}

我的Jquery是:

代码语言:javascript
运行
复制
 $.ajax({
  url: url_to_json,
  async: false,
  crossDomain: true,
  contentType: 'application/json; charset=utf-8',
  dataType: 'jsonp',
  timeout: 5000,
  success: function (data, status) {
    if (data !== undefined && data.post !== undefined) {
      $('#news').append('<div id="nimg" style="background-image: url(' + data.post.attachments[images].full.url + ')"></div><div id="newstext"><div id="newstitle">' + data.post.title + '</div><div>' + data.post.content + '</div></div>');
    }
  },
  error: function () {
    output.html('<h1 class="error">There was an error loading the data.</h2>');
  }
});

请帮帮忙,我是JQUERY的新手。我真的很感谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-27 18:48:09

试一试:

代码语言:javascript
运行
复制
data.post.attachments[0]['images'].full.url

或者说:

代码语言:javascript
运行
复制
data.post.attachments[0].images.full.url
票数 3
EN

Stack Overflow用户

发布于 2013-06-27 18:48:41

这里有两个问题

.replace(/\\\//, '/')属性访问器错误,应该是

  1. 是javascript转义的,你需要取消转义它

所以它应该是

代码语言:javascript
运行
复制
$('#news').append('<div id="nimg" style="background-image: url(' + data.post.attachments[0].images.full.url.replace(/\\\//, '/') + ')"></div><div id="newstext"><div id="newstitle">' + data.post.title + '</div><div>' + data.post.content + '</div></div>');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17341036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档