【问题标题】:Not able to get values from the API using jquery无法使用 jquery 从 API 获取值
【发布时间】:2018-01-06 17:26:04
【问题描述】:

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

这是我的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>

以下是我网页的快照。每当单击按钮时,我都会尝试在白色区域&lt;div id="quote" class="well position-well"&gt; 中打印随机报价。

【问题讨论】:

    标签: javascript jquery html json ajax


    【解决方案1】:

    您的服务器的“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/
    

    【讨论】:

    • 感谢您的回复@shawn。我没有完全明白你所说的,因为我直到现在才知道 ACAO 和 CORS 是什么。根据我的理解,我必须在客户端尝试从 API 中获取资源,该 API 不在我的服务器上,而是由其他人在不同的服务器上构建和部署的,并且没有货币化。那么服务器端的响应中是否没有添加CORS?这是你的意思吗?还有,我该如何调试我的页面?
    • 此外,当我从postman 触发 API 时,我得到了响应。您能否告诉我如何以及在哪里检查 ACAO 和 CORS 以及如何调试我的页面?因为我在浏览器控制台上看不到任何内容
    • 您可以使用 Chrome 或 Firefox,右键单击,检查,触发 http 请求,然后查看“网络”选项卡。
    • 嗯,我觉得你遇到的问题有点复杂。不确定的事情太多了。你在哪里查询url,在同一个域下还是跨域?如果要进行跨域查询,则应涉及 JSONP,来自第三个 http 客户端(如 postman)的 JSONP 请求应为 http://quotesondesign.com/wp-json/posts?filter[orderBy]=rand&amp;filter[posts_per_page]=1&amp;_jsonp=xxxxxxx
    【解决方案2】:

    由于您正在发出跨域请求,因此您需要使用第二个示例。只需将 &amp;_jsonp=? 添加到 API URL。如果您的网页托管在 https 上(如 stacksn-ps),那么 API URL 也必须是 https。

    $(document).ready(function() {
      $("#click").on("click", function() {
        $.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=?", function(data) {
          console.log(data);
        });
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <button type="button" id="click" class="button-shape">Button</button>

    我还是不明白回调参数的作用。

    您正在从(显然)不允许跨源请求的 API 请求 JSON 数据。但他们提供了一种解决方法:如果添加 _jsonp=myfunc,则服务器返回 JSONP 而不是 JSON,后者本质上是包装 JSON 数据的 JavaScript 代码。 jQuery.ajax 解释详情。

    【讨论】:

    • 感谢@salman 的回答。查询正在运行,但我无法访问随机引号,因为 API 仅接受 orderByposts_per_page 作为参数。如果我使用 'jsonp' 参数,则只会打印第一个引号。
    • 我尝试添加回调参数$.getJSON("https://quotesondesign.com/wp-json/posts?filter[orderBy]=2&amp;filter[posts_per_page]=1&amp;callback=''&amp;_jsonp=?", 我得到的报价响应是默认响应。但是,当我尝试使用相同的 URL 但使用 jsonp 从邮递员触发 API 时,我得到了所需的随机响应。
    • @GauravThantry 你不擅长复制粘贴。它应该是orderby,而不是orderBy。更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2017-12-20
    • 1970-01-01
    • 2013-03-22
    • 2015-03-13
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多