我发送的请求会返回以下内容:
{
"title": "Recent Uploads",
"link": "http://www.flickr.com/photos/",
"description": "",
"generator": "http://www.flickr.com/",
"items": [
{
"title": "Title 1",
"link": "http://www.flickr.com/photos/123"
} ]
}
然而,当我尝试访问其中的内容时,我得到一个'未定义'值:
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
var rsp = xml.rspTxt
console.log(rsp.title);
}
}
我做错了什么?据我所知,这应该有用......
编辑:Fiddle。
答案 0 :(得分:1)
这是因为您的JSON根本不是有效的JSON ..
var test = {
"title": "Recent Uploads",
"link": "http://www.flickr.com/photos/",
"description": "",
"generator": "http://www.flickr.com/",
"items": {
"title": "Title 1",
"link": "http://www.flickr.com/photos/123"
}
};
console.log(test.title);
这样可以正常工作,并在控制台中生成“最近上传”。你不能在最后有'['there和','。
答案 1 :(得分:0)
正如其他人之前所说,您从该通话中获得的JSON回复无效。
{
"title": "Recent Uploads tagged potato",
"link": "https://www.flickr.com/photos/tags/potato/",
"description": "",
"modified": "2015-10-14T09:17:25Z",
"generator": "https://www.flickr.com/",
"items": [
{
"title": "French fries",
"link": "https://www.flickr.com/photos/87608453@N06/22159555595/",
"media": {"m":"https://farm1.staticflickr.com/758/22159555595_0a1ba32e44_m.jpg"},
"date_taken": "2008-06-18T16:50:34-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/87608453@N06/\">Chulpa<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/87608453@N06/22159555595/\" title=\"French fries\"><img src=\"https://farm1.staticflickr.com/758/22159555595_0a1ba32e44_m.jpg\" width=\"240\" height=\"160\" alt=\"French fries\" /><\/a><\/p> <p>Heap of crisp French fries - detail<\/p>",
"published": "2015-10-14T09:17:25Z",
"author": "nobody@flickr.com (Chulpa)",
"author_id": "87608453@N06",
"tags": "hot detail macro closeup studio fastfood tasty sidedish frenchfries chips crisp potato snack junkfood takeaway portion sliced popular unhealthy baked roasted deepfried fattening accompaniment pommefrites takeoutfood"
},
{
"title": "potatowedges",
"link": "https://www.flickr.com/photos/64474673@N08/22131498326/",
"media": {"m":"https://farm1.staticflickr.com/700/22131498326_05d9f67dac_m.jpg"},
"date_taken": "2015-10-14T00:23:48-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/64474673@N08/\">amandahar<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/64474673@N08/22131498326/\" title=\"potatowedges\"><img src=\"https://farm1.staticflickr.com/700/22131498326_05d9f67dac_m.jpg\" width=\"240\" height=\"240\" alt=\"potatowedges\" /><\/a><\/p> ",
"published": "2015-10-14T07:23:48Z",
"author": "nobody@flickr.com (amandahar)",
"author_id": "64474673@N08",
"tags": "potato potatowedges"
},
{
"title": "Shadows and Potato van in Preston",
"link": "https://www.flickr.com/photos/tonyworrall/21968606450/",
"media": {"m":"https://farm1.staticflickr.com/758/21968606450_3a9c70e5bc_m.jpg"},
"date_taken": "2015-09-30T13:12:20-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/tonyworrall/\">Tony Worrall Foto<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/tonyworrall/21968606450/\" title=\"Shadows and Potato van in Preston\"><img src=\"https://farm1.staticflickr.com/758/21968606450_3a9c70e5bc_m.jpg\" width=\"240\" height=\"171\" alt=\"Shadows and Potato van in Preston\" /><\/a><\/p> <p>© 2015 Tony Worrall<\/p>",
"published": "2015-10-14T06:34:58Z",
"author": "nobody@flickr.com (Tony Worrall Foto)",
"author_id": "10089490@N06",
"tags": "county street uk england people urban food lunch stream tour shadows open place northwest unitedkingdom candid country north visit location lancashire eat potato queue area buy preston van northern update streetfood attraction lancs welovethenorth :copyright:2015tonyworrall"
},
{
"title": "thực phẩm giàu dinh dưỡng",
"link": "https://www.flickr.com/photos/134900096@N07/22156604725/",
"media": {"m":"https://farm6.staticflickr.com/5751/22156604725_882ca5642d_m.jpg"},
"date_taken": "2012-08-06T16:59:38-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/134900096@N07/\">hoaithuong0115<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/134900096@N07/22156604725/\" title=\"thực phẩm giàu dinh dưỡng\"><img src=\"https://farm6.staticflickr.com/5751/22156604725_882ca5642d_m.jpg\" width=\"240\" height=\"160\" alt=\"thực phẩm giàu dinh dưỡng\" /><\/a><\/p> <p>Xem: <a href=\"http://emdep.vn/thuc-don-giam-can-s275.htm\" rel=\"nofollow\">emdep.vn/thuc-don-giam-can-s275.htm<\/a><\/p>",
"published": "2015-10-14T06:25:03Z",
"author": "nobody@flickr.com (hoaithuong0115)",
"author_id": "134900096@N07",
"tags": "red food orange green apple mushroom fruits vegetables tomato pepper healthy natural display market sweet tomatoes olive banana supermarket lettuce potato pineapple asparagus crop snack carrot pear cauliflower garlic produce onion feed organic grocery fiber eats edible goodies eatable groceries arrangement grape combination nutrition lowfat nourishment nutrients nonfat provision lowcalorie nutriment"
},
{
"title": "",
"link": "https://www.flickr.com/photos/underthetuliptree/21959671820/",
"media": {"m":"https://farm6.staticflickr.com/5803/21959671820_f60c0cd98f_m.jpg"},
"date_taken": "2012-07-12T14:22:38-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/underthetuliptree/\">julianna smith<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/underthetuliptree/21959671820/\" title=\"\"><img src=\"https://farm6.staticflickr.com/5803/21959671820_f60c0cd98f_m.jpg\" width=\"160\" height=\"240\" alt=\"\" /><\/a><\/p> <p>Potato cakes with onions and spinach<\/p>",
"published": "2015-10-13T22:42:18Z",
"author": "nobody@flickr.com (julianna smith)",
"author_id": "34306073@N07",
"tags": "food cakes dinner vegan smith potato julianna"
},
{
"title": "Prague 14",
"link": "https://www.flickr.com/photos/131811220@N07/22106050326/",
"media": {"m":"https://farm6.staticflickr.com/5636/22106050326_67983e253b_m.jpg"},
"date_taken": "2015-10-04T14:44:43-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/131811220@N07/\">MichaelWard82<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/131811220@N07/22106050326/\" title=\"Prague 14\"><img src=\"https://farm6.staticflickr.com/5636/22106050326_67983e253b_m.jpg\" width=\"240\" height=\"160\" alt=\"Prague 14\" /><\/a><\/p> ",
"published": "2015-10-13T10:46:54Z",
"author": "nobody@flickr.com (MichaelWard82)",
"author_id": "131811220@N07",
"tags": "food republic czech prague potato twirl"
},
{
"title": "My Dinner",
"link": "https://www.flickr.com/photos/sarahjdhue/21941657920/",
"media": {"m":"https://farm6.staticflickr.com/5701/21941657920_53387b668c_m.jpg"},
"date_taken": "2015-10-11T15:17:25-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/sarahjdhue/\">SarahJDhue<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/sarahjdhue/21941657920/\" title=\"My Dinner\"><img src=\"https://farm6.staticflickr.com/5701/21941657920_53387b668c_m.jpg\" width=\"240\" height=\"181\" alt=\"My Dinner\" /><\/a><\/p> <p>Weinerschnitzel ala Holstein -Sarah J Dhue<\/p>",
"published": "2015-10-13T08:11:25Z",
"author": "nobody@flickr.com (SarahJDhue)",
"author_id": "92310074@N06",
"tags": "red food pancakes dinner restaurant illinois october yum egg cellphone samsung palace il potato german ala cabbage fried dreamland applesauce holstein veal weinerschnitzel galaxys6 sarahjdhuephotos sarahjdhue"
},
{
"title": "Open Faced at Old Vine Cafe",
"link": "https://www.flickr.com/photos/55017688@N00/22136102231/",
"media": {"m":"https://farm6.staticflickr.com/5651/22136102231_984461d378_m.jpg"},
"date_taken": "2015-09-19T22:02:02-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/55017688@N00/\">deeeelish<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/55017688@N00/22136102231/\" title=\"Open Faced at Old Vine Cafe\"><img src=\"https://farm6.staticflickr.com/5651/22136102231_984461d378_m.jpg\" width=\"240\" height=\"149\" alt=\"Open Faced at Old Vine Cafe\" /><\/a><\/p> <p>Fresh Biscuit, Chicken Sausage, Scrambled Eggs, Chipotle Gravy & Queso Fresco with Potatoes <br /> <br /> Costa Mesa, CA<\/p>",
"published": "2015-10-13T04:30:52Z",
"author": "nobody@flickr.com (deeeelish)",
"author_id": "55017688@N00",
"tags": "gravy potato eggs quesofresno"
},
{
"title": "Omelette at Old Vine Cafe",
"link": "https://www.flickr.com/photos/55017688@N00/21938083198/",
"media": {"m":"https://farm1.staticflickr.com/593/21938083198_046a230259_m.jpg"},
"date_taken": "2015-09-19T22:02:47-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/55017688@N00/\">deeeelish<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/55017688@N00/21938083198/\" title=\"Omelette at Old Vine Cafe\"><img src=\"https://farm1.staticflickr.com/593/21938083198_046a230259_m.jpg\" width=\"240\" height=\"156\" alt=\"Omelette at Old Vine Cafe\" /><\/a><\/p> <p>Costa Mesa, CA<\/p>",
"published": "2015-10-13T04:30:54Z",
"author": "nobody@flickr.com (deeeelish)",
"author_id": "55017688@N00",
"tags": "potato eggs omelette"
},
{
"title": "Chicken",
"link": "https://www.flickr.com/photos/jorkew/22106677661/",
"media": {"m":"https://farm6.staticflickr.com/5710/22106677661_00d43a64bc_m.jpg"},
"date_taken": "2015-09-05T00:00:00-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/jorkew/\">Jorkew<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/jorkew/22106677661/\" title=\"Chicken\"><img src=\"https://farm6.staticflickr.com/5710/22106677661_00d43a64bc_m.jpg\" width=\"160\" height=\"240\" alt=\"Chicken\" /><\/a><\/p> <p>Kodak Ektar 100<br /> Canon eos 1<br /> Canon EF 24-70mm f/2.8 L USM<br /> <br /> A few test shots from the new camera. <br /> <br /> <a href=\"http://jorkew.com/\" rel=\"nofollow\">Website<\/a> | <a href=\"http://facebook.com/jorkewphotography\" rel=\"nofollow\">Facebook<\/a><\/p>",
"published": "2015-10-12T20:40:00Z",
"author": "nobody@flickr.com (Jorkew)",
"author_id": "28568131@N06",
"tags": "camera red food chicken film analog canon photography eos 1 potatoes oven kodak kip potato l produce 100 usm filmcamera 135 done canoneos f28 canonef2470mmf28lusm ef baked eos1 ektar foodphotography 2470mm analogcamera llens kodak100 filmisnotdead canoneos1 canonllens kodakektar canon2470mmf28 kodakcolorfilm canonef2470mm ektar100 canonanalogcamera staybrokeshootfilm canoneos1analog canoneos1filmcamera"
},
{
"title": "Alice in Wonderland Pitstop",
"link": "https://www.flickr.com/photos/magicattic88/21491271583/",
"media": {"m":"https://farm1.staticflickr.com/746/21491271583_5113d90565_m.jpg"},
"date_taken": "2015-10-07T00:00:00-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/magicattic88/\">Darling Starlings<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/magicattic88/21491271583/\" title=\"Alice in Wonderland Pitstop\"><img src=\"https://farm1.staticflickr.com/746/21491271583_5113d90565_m.jpg\" width=\"240\" height=\"240\" alt=\"Alice in Wonderland Pitstop\" /><\/a><\/p> <p>After seeing Terry Wogan in the Waterstones tent - CheltLitFest 2015 - Jue [https://www.flickr.com/photos/pyride] and I wandered around a bit - we found this lovely little Alice in Wonderland themed café on the Festival site. It called for a Penelope Pitstop of crunchy lattice chips with dips and a coffee each - yum. Just what was needed :)).<\/p>",
"published": "2015-10-12T16:17:19Z",
"author": "nobody@flickr.com (Darling Starlings)",
"author_id": "8649118@N08",
"tags": "food coffee festival hearts town cafe penelope poinsettia hats literature chips potato pitstop cheltenham lattice aliceinwonderland dips imperialgardens 2015 candlebra bigteacup cheltlitfest"
},
{
"title": "Giant potatomobile",
"link": "https://www.flickr.com/photos/asciident/22079616896/",
"media": {"m":"https://farm6.staticflickr.com/5765/22079616896_c1bedd0a4a_m.jpg"},
"date_taken": "2015-10-04T17:10:42-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/asciident/\">asciident<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/asciident/22079616896/\" title=\"Giant potatomobile\"><img src=\"https://farm6.staticflickr.com/5765/22079616896_c1bedd0a4a_m.jpg\" width=\"240\" height=\"202\" alt=\"Giant potatomobile\" /><\/a><\/p> <p>Near Wendell, Idaho<br /> <br /> Excuse the thumb, I was in a hurry to catch the potatomobile!<\/p>",
"published": "2015-10-12T06:59:23Z",
"author": "nobody@flickr.com (asciident)",
"author_id": "12616001@N00",
"tags": "idaho potato wendell potatomobile"
},
{
"title": "Giant potatomobile",
"link": "https://www.flickr.com/photos/asciident/22115860151/",
"media": {"m":"https://farm1.staticflickr.com/646/22115860151_c18609aa78_m.jpg"},
"date_taken": "2015-10-04T17:10:45-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/asciident/\">asciident<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/asciident/22115860151/\" title=\"Giant potatomobile\"><img src=\"https://farm1.staticflickr.com/646/22115860151_c18609aa78_m.jpg\" width=\"186\" height=\"240\" alt=\"Giant potatomobile\" /><\/a><\/p> <p>Near Wendell, Idaho<\/p>",
"published": "2015-10-12T06:59:24Z",
"author": "nobody@flickr.com (asciident)",
"author_id": "12616001@N00",
"tags": "idaho potato wendell potatomobile"
},
{
"title": "Giant potatomobile",
"link": "https://www.flickr.com/photos/asciident/21917906328/",
"media": {"m":"https://farm1.staticflickr.com/684/21917906328_fb679fb950_m.jpg"},
"date_taken": "2015-10-04T17:10:44-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/asciident/\">asciident<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/asciident/21917906328/\" title=\"Giant potatomobile\"><img src=\"https://farm1.staticflickr.com/684/21917906328_fb679fb950_m.jpg\" width=\"240\" height=\"180\" alt=\"Giant potatomobile\" /><\/a><\/p> <p>Near Wendell, Idaho<\/p>",
"published": "2015-10-12T06:59:24Z",
"author": "nobody@flickr.com (asciident)",
"author_id": "12616001@N00",
"tags": "idaho potato wendell potatomobile"
},
{
"title": "Giant potatomobile",
"link": "https://www.flickr.com/photos/asciident/22093267702/",
"media": {"m":"https://farm1.staticflickr.com/749/22093267702_5f51f6e60e_m.jpg"},
"date_taken": "2015-10-04T17:10:47-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/asciident/\">asciident<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/asciident/22093267702/\" title=\"Giant potatomobile\"><img src=\"https://farm1.staticflickr.com/749/22093267702_5f51f6e60e_m.jpg\" width=\"240\" height=\"180\" alt=\"Giant potatomobile\" /><\/a><\/p> <p>Near Wendell, Idaho<\/p>",
"published": "2015-10-12T06:59:25Z",
"author": "nobody@flickr.com (asciident)",
"author_id": "12616001@N00",
"tags": "idaho potato wendell potatomobile"
},
{
"title": "Giant potatomobile",
"link": "https://www.flickr.com/photos/asciident/21917907758/",
"media": {"m":"https://farm6.staticflickr.com/5744/21917907758_f1130f8768_m.jpg"},
"date_taken": "2015-10-04T17:10:40-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/asciident/\">asciident<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/asciident/21917907758/\" title=\"Giant potatomobile\"><img src=\"https://farm6.staticflickr.com/5744/21917907758_f1130f8768_m.jpg\" width=\"240\" height=\"200\" alt=\"Giant potatomobile\" /><\/a><\/p> <p>Near Jerome, Idaho<\/p>",
"published": "2015-10-12T06:59:23Z",
"author": "nobody@flickr.com (asciident)",
"author_id": "12616001@N00",
"tags": "idaho potato jerome potatomobile"
},
{
"title": "Twister Potato Chip",
"link": "https://www.flickr.com/photos/jimmychuah/22104255305/",
"media": {"m":"https://farm6.staticflickr.com/5714/22104255305_abe56671da_m.jpg"},
"date_taken": "2015-09-11T16:22:14-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/jimmychuah/\">Jimmy Chuah<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/jimmychuah/22104255305/\" title=\"Twister Potato Chip\"><img src=\"https://farm6.staticflickr.com/5714/22104255305_abe56671da_m.jpg\" width=\"240\" height=\"160\" alt=\"Twister Potato Chip\" /><\/a><\/p> ",
"published": "2015-10-12T05:23:56Z",
"author": "nobody@flickr.com (Jimmy Chuah)",
"author_id": "67226751@N02",
"tags": "travel food holiday stall korea potato snack seoul koreanfood myeongdong 2015"
},
{
"title": "Potato Rolled Shrimp",
"link": "https://www.flickr.com/photos/jimmychuah/21917351019/",
"media": {"m":"https://farm1.staticflickr.com/694/21917351019_d93527d6d7_m.jpg"},
"date_taken": "2015-09-11T17:18:27-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/jimmychuah/\">Jimmy Chuah<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/jimmychuah/21917351019/\" title=\"Potato Rolled Shrimp\"><img src=\"https://farm1.staticflickr.com/694/21917351019_d93527d6d7_m.jpg\" width=\"240\" height=\"160\" alt=\"Potato Rolled Shrimp\" /><\/a><\/p> ",
"published": "2015-10-12T05:24:05Z",
"author": "nobody@flickr.com (Jimmy Chuah)",
"author_id": "67226751@N02",
"tags": "travel food holiday stall shrimp korea potato snack seoul koreanfood myeongdong 2015"
},
{
"title": "fzk_188_28082015_09\'48",
"link": "https://www.flickr.com/photos/eduard_buerge-rafz/21901809028/",
"media": {"m":"https://farm1.staticflickr.com/619/21901809028_7637fa8cd4_m.jpg"},
"date_taken": "2015-08-28T09:48:46-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/eduard_buerge-rafz/\">eduard43<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/eduard_buerge-rafz/21901809028/\" title=\"fzk_188_28082015_09\'48\"><img src=\"https://farm1.staticflickr.com/619/21901809028_7637fa8cd4_m.jpg\" width=\"240\" height=\"135\" alt=\"fzk_188_28082015_09\'48\" /><\/a><\/p> <p>Die Kartoffel (Solanum tuberosum).<br /> The potato (Solanum tuberosum).<\/p>",
"published": "2015-10-11T12:22:17Z",
"author": "nobody@flickr.com (eduard43)",
"author_id": "40303029@N08",
"tags": "potato kartoffeln naturalproducts solanumtuberosum potatoplants naturprodukte kartoffelstauden"
},
{
"title": "fzk_189_28082015_09\'48",
"link": "https://www.flickr.com/photos/eduard_buerge-rafz/22077208832/",
"media": {"m":"https://farm1.staticflickr.com/761/22077208832_e1616a4351_m.jpg"},
"date_taken": "2015-08-28T09:48:51-08:00",
"description": " <p><a href=\"https://www.flickr.com/people/eduard_buerge-rafz/\">eduard43<\/a> posted a photo:<\/p> <p><a href=\"https://www.flickr.com/photos/eduard_buerge-rafz/22077208832/\" title=\"fzk_189_28082015_09\'48\"><img src=\"https://farm1.staticflickr.com/761/22077208832_e1616a4351_m.jpg\" width=\"135\" height=\"240\" alt=\"fzk_189_28082015_09\'48\" /><\/a><\/p> <p>Die Kartoffel (Solanum tuberosum).<br /> The potato (Solanum tuberosum).<\/p>",
"published": "2015-10-11T12:22:16Z",
"author": "nobody@flickr.com (eduard43)",
"author_id": "40303029@N08",
"tags": "potato kartoffeln naturalproducts solanumtuberosum potatoplants naturprodukte kartoffelstauden"
}
]
}
你可以在jsonlint.com上检查出来,数组中的最后两个对象有不好的转义和其他问题......如果我手动删除它们,其余的响应都是有效的JSON。你无法解决这个问题,但必须要求flickr提供有效的JSON。