Javascript jquery在JSON api上运行
我正在尝试循环浏览《纽约时报》的搜索API。生成的JSON如下所示:Javascript jquery在JSON api上运行,javascript,jquery,json,Javascript,Jquery,Json,我正在尝试循环浏览《纽约时报》的搜索API。生成的JSON如下所示: { "offset": "0", "results": [{ "body": "NEW ORLEANS — The hemorrhaging well that has spilled millions of gallons of oil into the Gulf of Mexico remained capped for a second day Friday, providin
{
"offset": "0",
"results": [{
"body": "NEW ORLEANS — The hemorrhaging well that has spilled millions of gallons of oil into the Gulf of Mexico remained capped for a second day Friday, providing some hope of a long-term solution to the environmental disaster. Live video from the seabed Friday morning showed that all was quiet around the top of the well, suggesting the test",
"byline": "By CAMPBELL ROBERTSON and HENRY FOUNTAIN",
"date": "20100717",
"title": "Oil Spill Capped for a Second Day, Offering Some Hope",
"url": "http:\/\/www.nytimes.com\/2010\/07\/17\/us\/17spill.html"
}, {
"body": "GALVESTON, Tex. — The crayons and paper were out, but not too many children made it to family day at the Ocean Star Offshore Drilling Rig and Museum . Granted, the exhibits of pipelines and seismic vessels may have been over the heads of many grade-schoolers. And despite a few cheerful displays about marine life around rigs and all the bounty",
"byline": "By MELENA RYZIK",
"date": "20100716",
"title": "Texas Remains Stoic as Spill Hits Its Shores",
"url": "http:\/\/www.nytimes.com\/2010\/07\/16\/us\/16galveston.html"
}
(这只是较大的“结果”集中的两个)
下面是我正在使用的代码(删除API密钥)
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api key=key_删除“+”&callback=?”,函数(e){
$.each(如结果、函数(){
$(“#光滑”)。附加到(
''+这个.标题+''的
);
});
});
我觉得这样可以循环打印所有的故事标题,但事实并非如此。Chrome开发工具返回两个错误:“未捕获的SyntaxError:意外标记”和“资源被解释为脚本,但使用MIME类型text/plain传输”
我仍然在努力完成JSON的Jquery解析,所以我确信我做的一些简单的事情是错误的。任何帮助都将不胜感激。中的回调实际上接受两个参数,indexInArray
和valueOfElement
,因此,当您处于闭包中时,您需要像这样引用这些参数:
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api-key=KEY_REMOVED'+'&callback=?', function(e) {
$.each(e.results, function(indexInArray, valueOfElement) {
$('#slippery').append(
'<h2>' + valueOfElement.title + '</h2>'
);
});
});
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api key=key_删除“+”&callback=?”,函数(e){
$.each(如结果、函数(indexInArray、valueOfElement){
$(“#光滑”)。追加(
''+valueOfElement.title+''的值
);
});
});
我想您想要的是append
方法,而不是appendTo
。AppendTo接受一个选择器。追加将数据添加到当前选定的项。我认为您希望将内容添加到“#slaid”
div中,而不是将“#slaid”
附加到某个内容中
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api-key=KEY_REMOVED'+'&callback=?', function(e) {
$.each(e.results, function() {
$('#slippery').append(
'<h2>' + this['title'] + '</h2>'
);
});
});
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api key=key_删除“+”&callback=?”,函数(e){
$.each(如结果、函数(){
$(“#光滑”)。追加(
''+此['title']+''项
);
});
});
试试这个表单:
$(obj).each(function(iteration, value) { /* ... */ });
看起来您实际上使用的是jsonp,而不仅仅是json。您将需要更改您的请求,而不是将
$.ajax()
与数据类型一起使用:“jsonp”
从jQ API文档中:
JSONP
如果URL中包含字符串“callback=?”,则请求将被视为JSONP。有关更多详细信息,请参阅$.ajax()中对jsonp数据类型的讨论。
您可能希望跳过使用$。each()
函数,只需使用常规的for
循环即可。请参阅以获取解释
基本上
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api-key=KEY_REMOVED'+'&callback=?', function(e) {
$slippery = $('#slippery');
for ($i = 0, $j = e.results.length; $i < $j; $i++) {
$slippery.appendTo('<h2>' + e.results[$i].title + '</h2>');
}
});
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api key=key_删除“+”&callback=?”,函数(e){
$湿滑=$(“#湿滑”);
对于($i=0,$j=e.results.length;$i<$j;$i++){
$slidy.appendTo(“”+e.results[$i].title+“”);
}
});
将更快地执行。发现问题在于语法错误,原因是NYTimes API不会返回JSONP。必须手动包装它。这是另一天的问题。注意:更正了我代码中的一个输入错误(append改为appendTo),但没有作为旁注解决问题,这是一个JSONP请求,而不仅仅是一个JSON请求。错误。您不需要使用这些参数。这不是问题所在。
$(这)
不是语法错误吗这应该是一个关联数组,而不是选择器或DOM元素。@R.Bemrose是正确的。当您将其包装到jQuery对象中时,您试图查找对象的title
属性,该属性不存在。我可以想象,jsonp是理想的,因为请求可能是向外部域发出的。此外,OP正在得到一个结果,只是没有得到解析。你说得对,但它仍然不起作用。改回append,仍然是nada。@dan sinker-您可以尝试使用this['title']
作为访问该属性的替代方法。否则,请尝试记录或跟踪此
,并查看它包含的内容。如果您将其更改为类似于$('#slaid').append('Test')代码>您是否获得了正确的H2数量?返回数据格式可能有问题。如果您解决了,请告诉我
$.getJSON('http://api.nytimes.com/svc/search/v1/article?format=json&query=oil%2C+bp%2C+gulf+of+mexico&api-key=KEY_REMOVED'+'&callback=?', function(e) {
$slippery = $('#slippery');
for ($i = 0, $j = e.results.length; $i < $j; $i++) {
$slippery.appendTo('<h2>' + e.results[$i].title + '</h2>');
}
});