从本地计算机运行程序时,如何在此URL中检索数据?

时间:2019-04-24 13:41:48

标签: javascript json ajax local-storage

我正在测试一个网站,并试图通过我的本地计算机在我网站的URL中运行数据,但是该网站无法正常工作。

function getQuotes() {
  return $.ajax({
    headers: {
      Accept: "application/json"
    },
    url: 'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json',
    success: function(jsonQuotes) {
      if (typeof jsonQuotes === 'string') {
        quotesData = JSON.parse(jsonQuotes);
        console.log('quotesData');
        console.log(quotesData);
      }
    }
  });
}

当我从IDE运行站点时,引用框应填充JSON数据。

1 个答案:

答案 0 :(得分:-1)

根据您使用的jQuery版本,您可以简单地使用<div class="sticky-top">...</div>

这里是一个例子:

position: sticky;
$.getJSON()
(function() {

  var url = "https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json";

  $.getJSON( url)
    .done(function( data ) {
      // Ensure data.quotes is an array
      if(Array.isArray(data.quotes)) {
        // Append each quote in the quote box
        $.each( data.quotes, function(i, item) {
          var quoteText = item.quote + " (" + item.author + ")";
          $("#quotesBox").append("<blockquote>" + quoteText + "</blockquote>");
        });
      }      
    });

})();

相关问题