使用 JavaScript 解析 json 失败
2016-04-21
74
当我调用一个返回
json
格式(标题和内容)随机引号的
api
时,我可以正常收到
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);
输出:无。
3个回答
尝试 console.log(data[0].content);
or hor
2016-04-21
我认为问题在于您正在进行字符串化而不是解析
console.log(JSON.parse(data));
编辑 : 问题中的示例也存在其他一些问题。这是一个格式正确的 ajax 请求:
$.ajax({
type: 'GET',
dataType: 'json',
url: 'URL_HERE',
success: function(data) {
console.log(data.content);
}
});
以及一个有效示例 https://jsfiddle.net/1fjqgajk/8/
Daric
2016-04-21
$.ajax({url:"quotesondesign.com/wp-json/posts? filter[orderby]=rand&filter[posts_per_page]=1",type:'POST',dataType="json",success: function(data){
// if you dont wanto to use dataType use eval;
//var jsonObj=eval(data)
}});
Tosif
2016-04-21