當我打電話的api
,在json
格式(標題和內容)返回一個隨機的報價,我收到json
就好:用JavaScript解析JSON失敗
ajax({ url: 'quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1', type: 'json' },
function(data) {
console.log((JSON.stringify(data)));
console.log(data.content);
console.log((JSON.stringify(data)));
輸出:
[{
"ID":1562,
"title":"Michael Bierut",
"content":"Most of the mediocre design today comes from designers who are faithfully doing as they were taught in school: they worship at the altar of the visual.\n",
"link":"http:\/\/quotesondesign.com\/michael-bierut-3\/",
"custom_meta":{"Source":"article"}
}]
但是console.log(data.content);
輸出:無。
根JSON值不具有' 「內容」'屬性來匹配'data.content'。它看起來'data'是一個數組--' console.log(data [0] .content)'等等([Access/process(nested)objects,arrays or JSON](http://stackoverflow.com/questions/) 11922383/access-process-nested-objects-arrays-or-json)) –
@RayonDabre目前還不清楚這是jQuery。儘管有一些相似之處,但這些參數似乎與'jQuery.ajax()'不兼容。 –
@JonathanLonowski,真的! – Rayon