javascript - 无法使用 jquery 从 API 获取值

标签 javascript jquery html json ajax

我无法从 Random quote API 获取值。我不知道这个问题发生在哪里。是在取内容的时候还是返回到前端的时候。如果我有权访问日志,就可以弄清楚这一点。而且,我在互联网上免费找到了这个 API。我还是不明白回调参数的作用。 请引用下面的屏幕截图

Random Quote API jquery

这是我的 html 代码

$(document).ready(function() {
  $("#click").on("click", function() {
    $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderBy]=rand&filter[posts_per_page]=1&callback=", function(key) {
      $("#quote").append(key[0].content + "<br><p> - " + key[0].title + "</p>");
    });
  });
});
.position-message {
  margin-left: 25%;
  margin-right: 25%;
  margin-top: 10%;
  margin-bottom: 20%;
}

.background {
  background-color: maroon;
}

.button-shape {
  border-radius: 50%;
  width: 50px;
  height: 50px;
  margin: auto;
}

.fa-size {}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&amp;lang=en" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container-fluid">
  <div id="quote" class="well position-message"></div>
  <br>
  <br>
  <div class="row text-center">
    <button type="button" id="click" class="button-shape"><i class="fa fa-refresh fa-2x"></i></button>
  </div>
</div>

下面是我的网页的快照。我正在尝试在白色区域 <div id="quote" class="well position-well"> 打印随机引用每当单击按钮时。

Random Quote webpage

最佳答案

Seems that there is something wrong with your server's "Access-Control-Allow-Origin".

抱歉造成误导,http://null 似乎是由 stackoverflow 的 Run code snippet 引起的。

I made a working copy (no code change besides change the `http` to `https`):

https://jsfiddle.net/qex5y16v/1/

关于javascript - 无法使用 jquery 从 API 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48129916/

相关文章:

javascript - 隐藏第一个子菜单

html - 我们怎样才能最小化 bootstrap 3 中的 gutter-width?

javascript - 单个函数可单独切换其他 div 内的多个 div

javascript - 清除浏览器后退按钮上的字段

javascript - 将数据放入对象中

jquery - 调整大小时 jquery-ui 和 bootstrap 3 之间的冲突

javascript - select2 标签 selectBox 的预选值

javascript - _references.js 是做什么用的?

jquery - Raphael 通过 ID 获取 Element

java - <小程序> 与 <对象>