【问题标题】:Ajax request returning undefined valueAjax 请求返回未定义的值
【发布时间】: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&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"}]}

【问题讨论】:

  • response 包含什么?
  • 您确定response.articles[0] 具有title 属性吗?
  • 我想看看你对我的规格的回应。
  • 傻笑,如果你把addDivOne(res);改成addDivOne("hello world");会发生什么
  • 在这种情况下,函数参数中的resone 与函数主体中的resone 拼写不同...或者您有另一个addDivOne 函数定义,其中包含一些类似这样的错误...在调用它之前尝试console.log(addDiveOne + "");,看看控制台中的函数是否符合您的期望

标签: javascript jquery html ajax


【解决方案1】:

在你的 loadArticle 函数中,console.log(res) 给你什么输出?

如果 'res' 有一些价值,那么将 resone 的值作为 undefined 是没有意义的。

还要确保 addDivOne 函数在你想要它之前没有被调用......就像在 loadArtice 中的 ajax 成功之前一样。

这可能会有所帮助.. 在 addDivOne 函数中添加一个条件,仅当 resone 具有某个值时才将值附加到 div ..(例如,如果 resone !== undefined 然后进行附加)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2018-09-12
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多