无法使用jQuery从API获取值 [英] Not able to get values from the API using jquery
问题描述
我无法从Random quote API
获取值.我不在发生此问题的地方.是在获取内容时还是在将其返回到前端时.如果我可以访问日志,本来可以弄清楚的.而且,我在互联网上免费找到了该API.我仍然不明白回调参数的作用.
请参考下面的屏幕截图
I am unable to fetch the values from a Random quote API
. I don't where this issue is happening. Is it while fetching the content or while returning it to the front end. Could have figured it out had I had access to the logs. And also, I found this API for free on the internet. I still don't understand what the callback param does.
please refer to the below screen shot
这是我的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&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">
中打印随机报价.
Below is a snapshot of my webpage. I am trying to print a random quote in the white area <div id="quote" class="well position-well">
whenever the button is clicked.
推荐答案
由于您在进行跨域请求,因此需要使用第二个示例.只需将&_jsonp=?
添加到API URL.如果您的网页托管在https(如stacksnippets)上,则API URL也必须是https.
Since you are making a cross-origin request you need to use the second example. Just add &_jsonp=?
to the API URL. If your webpage is hosted on https (like stacksnippets) then API URL must also be 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>
我仍然不明白回调参数的作用.
I still don't understand what the callback param does.
您正在从(显然)不允许跨源请求的API请求JSON数据.但是他们提供了一种解决方法:如果添加_jsonp=myfunc
,则服务器将返回JSONP而不是JSON,这实际上是包装JSON数据的JavaScript代码. jQuery.ajax
解释详细信息.
You are requesting JSON data from an API that (apparently) does not allow cross origin requests. But they have provided a work around: if you add _jsonp=myfunc
then the server returns JSONP instead of JSON, which is essentially JavaScript code that wraps JSON data. jQuery.ajax
explains the details.
这篇关于无法使用jQuery从API获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!