ajax同步变量未定义,Ajax请求返回未定义的值

我对外部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('

' + resone +

'

' +

'Delete' +

'

' +

'

');

}

问题在于,当我调用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值