【发布时间】:2016-11-28 11:17:01
【问题描述】:
我对外部 api 数据有一个简单的 JQuery Ajax 请求:
function loadArticle(){
$.ajax({
url: 'https://newsapi.org/v1/articles?source=techcrunch&apiKey=XXXX',
dataType: 'json',
type: 'GET'
}).done(function(response){
var res = response.articles[0].title;
console.log(res);
addDivOne(res);
}).fail(function(){
console.log('error no response')
}).always(function(){
console.log('always')
})
}
我需要将 "res" 值添加到另一个像这样在我的 HTML 页面中附加 div 的函数。
function addDivOne(resone) {
$("#content-div").append('<li class="new-div list-group-item" id="1">' + resone +
'<div class = "row" style="min-height: 75px;">' +
'<button type="button" class="btn pull-right btn-danger btn-close">Delete</button>' +
'</div>' +
'</li>');
}
问题是当我调用 addDivOne() 函数时,"resone" 的值是 undefined。 如何显示真正的 "res" 值?
附:我的回复:
{"status":"ok","source":"techcrunch","sortBy":"top","articles":[{"author":"Sarah Perez","title":"Black Friday online sales to hit a record-breaking $3 billion, over $1 billion from mobile","description":"Black Friday online shopping is continuing to grow, and this Friday was another record-breaking day. According to a new report out this evening from Adobe,..","url":"http://social.techcrunch.com/2016/11/25/black-friday-online-sales-to-hit-a-record-breaking-3-billion-over-1-billion-from-mobile/","urlToImage":"https://tctechcrunch2011.files.wordpress.com/2016/05/shutterstock_341086469.jpg?w=764&h=400&crop=1","publishedAt":"2016-11-26T00:44:17Z"},{"author":"Sarah Buhr","title":"Siren Care makes a “smart” sock to track diabetic health","description":"Diabetic health tracking startup Siren Care has created smart socks that use temperature sensors to detect inflammation -- and therefore injury -- in..","url":"http://social.techcrunch.com/2016/11/25/siren-care-makes-a-smart-sock-to-track-diabetic-health/","urlToImage":"https://tctechcrunch2011.files.wordpress.com/2016/11/1609_sirensocks_with-ap.jpg?w=764&h=400&crop=1","publishedAt":"2016-11-25T22:05:22Z"},{"author":"Connie Loizos","title":"Peter Thiel taps a principal at Founders Fund for Trump’s transition team","description":"Peter Thiel is famously loyal to his employees, and vice versa. Many of the dozens of people employed by his venture firm, Founders Fund, once worked for..","url":"http://social.techcrunch.com/2016/11/25/peter-thiel-taps-a-principal-at-founders-fund-for-trumps-transition-team/","urlToImage":"https://tctechcrunch2011.files.wordpress.com/2016/07/gettyimages-578544740.jpg?w=764&h=400&crop=1","publishedAt":"2016-11-25T16:21:10Z"},{"author":"Ingrid Lunden","title":"Payments provider Stripe has raised another $150M at a $9B valuation","description":"Stripe -- the company that lets websites and apps incorporate payments services by way of an API and a few lines of code -- has raised another round of..","url":"http://social.techcrunch.com/2016/11/25/payments-provider-stripe-has-raised-another-150-at-a-9b-valuation/","urlToImage":"https://tctechcrunch2011.files.wordpress.com/2015/06/kk.png?w=321&h=226&crop=1","publishedAt":"2016-11-25T12:16:17Z"}]}
【问题讨论】:
-
response包含什么? -
您确定
response.articles[0]具有title属性吗? -
我想看看你对我的规格的回应。
-
傻笑,如果你把
addDivOne(res);改成addDivOne("hello world");会发生什么 -
在这种情况下,函数参数中的
resone与函数主体中的resone拼写不同...或者您有另一个addDivOne函数定义,其中包含一些类似这样的错误...在调用它之前尝试console.log(addDiveOne + "");,看看控制台中的函数是否符合您的期望
标签: javascript jquery html ajax