當前我發送XMLHttpRequest
以檢索JSON
響應。我想將鏈接列爲「正常」,但我不確定如何引用它們以將它們放入我的HTML img元素列表中。這裏是JSON響應:如何使用返回的GET超鏈接作爲HTML中的img src鏈接
{
metadata: {
code: 200
message: "OK"
version: "v2.0"
} -
data: {
_links: {
self: {
href: "//z4photorankapi-a.akamaihd.net/customers/215757/media/recent/?count=20"
} -
first: {
href: "//z4photorankapi-a.akamaihd.net/customers/215757/media/recent/?count=20"
} -
next: {
href: "//z4photorankapi-a.akamaihd.net/customers/215757/media/recent/?next_id=czoyOToiLTHFkzE0NDM2OTE3MTQyMTPFkzIyNjIwOTU2NjMiOw~~&count=20"
} -
} -
_embedded: [20]
0: {
_links: {...
} -
type: "IMAGE"
source: "instagram"
source_id: "398002460025587218_31000928"
original_source: "http://instagr.am/p/WF_SDxjoYS/"
caption: "Packing my first thredUP bag! #thredup #womenslaunch #bigmoney"
video_url: null
share_url: "http://www.photorank.me/photos/demo/1232903472"
date_submitted: "2013-02-24T00:36:24+00:00"
favorite: false
location: {...
} -
images: {
square: "https://z3photorankmedia-a.akamaihd.net/media/b/s/d/bsdniy/square.jpg"
thumbnail: "https://z2photorankmedia-a.akamaihd.net/media/b/s/d/bsdniy/thumbnail.jpg"
mobile: "https://photorankmedia-a.akamaihd.net/media/b/s/d/bsdniy/mobile.jpg"
normal: "https://z3photorankmedia-a.akamaihd.net/media/b/s/d/bsdniy/normal.jpg"
original: "https://z1photorankmedia-a.akamaihd.net/media/b/s/d/bsdniy/original.jpg"
} -
_embedded: {...
} -
_forms: {
report: {
title: "Report photo?"
action: {
href: "//z2photorankapi-a.akamaihd.net/media/1232903472/reports"
} -
method: "POST"
fields: [3]
0: {
type: "email"
prompt: "Email"
name: "email"
value: ""
placeholder: "[email protected]"
} -
1: {
type: "short-text"
prompt: "Reason"
name: "reason"
value: ""
placeholder: ""
} -
2: {
type: "submit"
prompt: ""
name: "send"
value: "Report"
placeholder: ""
} -
-
} -
} -
_analytics: {
oid: "1232903472"
t: "media"
meta: [3]
0: "user_agent"
1: "event_type"
2: "is_mobile" -
} -
} -
1: {...
} -
2: {...
} -
3: {...
} -
4: {...
} -
5: {...
} -
6: {...
} -
7: {...
} -
8: {...
} -
9: {...
} -
10: {...
} -
11: {...
} -
12: {...
} -
13: {...
} -
14: {...
} -
15: {...
} -
16: {...
} -
17: {...
} -
18: {...
} -
19: {...
} -
-
} -
}
我的目標是把這些鏈接和列表在HTML文件中使用它們作爲src
爲img
標籤。我真的很感激任何幫助。
我在想我可以使用response.data._embedded[0].images.normal
來引用它,但這似乎並不奏效。
下面是HTML代碼,我有:
<ul id="photolist">
</ul>
這裏是我發送請求:
var getData = function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://photorankapi-a.akamaihd.net/customers/215757/media/recent/?auth_token=0a40a13fd9d531110b4d6515ef0d6c529acdb59e81194132356a1b8903790c18", true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var res = JSON.parse(xhr.response);
for (var y = 0; res.data._embedded.length; y++) {
var srclink = res.data._embedded[y].images.normal;
var listItem = document.createElement("li");
var image = document.createElement('img');
//add listItem to the photolist
document.getElementById('photolist').appendChild(listItem);
//add the image to the list item
listItem.innerHTML = image;
//make the image src = the hyperlink from the JSON response
image.src = srclink;
}
}
}
}
當代碼獲取到的嘗試,在這條線拉的圖像點:
var srclink = res.data._embedded[y].images.normal;
我得到如下回應:
遺漏的類型錯誤:無法讀取屬性「圖像」未定義的
xhr.onreadystatechange @ sliderscript.js:41
發表您的HTML代碼,所以我們可以幫助你剛剛添加 – LiranBo
。在此先感謝您的幫助! –