2016-11-28 192 views
0

我有外部API數據的簡單的jQuery Ajax請求:Ajax請求返回undefined值

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」 的值不確定。 如何顯示真實的「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&amp;h=400&amp;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&amp;h=400&amp;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&amp;h=400&amp;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&amp;h=226&amp;crop=1","publishedAt":"2016-11-25T12:16:17Z"}]} 
+0

添加從服務器的問題示例響應請。 – vijay

+1

'response'包含什麼? – LuudJacobs

+1

你確定'response.articles [0]'有'title'屬性嗎? –

回答

0

在loadArticle函數內部,console.log(res)爲您提供了哪些輸出?

如果'res'具有一定的價值,那麼將resone的值取爲undefined是沒有意義的。

還要確保addDivOne函數沒有在你想要它之前被調用。因爲在loadArtice內部沒有成功的Ajax之前。

這可能幫助.. 把一個條件addDivOne函數中,追加價值只有div的,如果resone有一定的價值。(前如果resone !== undefined然後執行追加)