我对外部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”值?
P.S。我的回答:
{"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"}]}
答案 0 :(得分:0)
在你的loadArticle函数中,console.log(res)给你什么作为输出?
如果'res'有一些值,那么将resone的值设为undefined是没有意义的。
还要确保在你想要它之前没有调用addDivOne函数..而不是在loadArtice中ajax成功之前。
这可能会有所帮助..
在addDivOne函数中放入一个条件,只有当resone有一些值时才将值附加到div ..(如果resone !== undefined
,则执行追加)