我正在使用 Ajax 访问字典 REST API。
$.ajax({
url: "http://api.wordnik.com//v4/word.json/cat/definitions?api_key=mykey&includeRelated=false&includeTags=false&limit=1",
dataType : 'json',
success: function(data) {
//called when successful
alert(data.word);
},
error: function(e) {
//called when there is an error
console.log(e.message);
}
});
响应:
[
{
"textProns": [],
"sourceDictionary": "ahd-legacy",
"exampleUses": [],
"relatedWords": [],
"labels": [],
"citations": [],
"word": "cat",
"text": "A small carnivorous mammal (Felis catus or F. domesticus) domesticated since early times as a catcher of rats and mice and as a pet and existing in several distinctive breeds and varieties.",
"sequence": "0",
"score": 0.0,
"partOfSpeech": "noun",
"attributionText": "from The American Heritage® Dictionary of the English Language, 4th Edition"
}
]
在我的测试示例中,我收到成功警报undefined
。我看到的大多数 Ajax 示例都使用循环,但我返回一个结果。如何从对象中提取word
和text
值?