无法使用jQuery从API获取值

编程入门 行业动态 更新时间:2024-10-27 08:27:07
本文介绍了无法使用jQuery从API获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法从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("quotesondesign/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="maxcdn.bootstrapcdn/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="cdnjs.cloudflare/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="//fonts.googleapis/css?family=Open+Sans:300,400,600,700&amp;lang=en" /> <script src="ajax.googleapis/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="maxcdn.bootstrapcdn/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("quotesondesign/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=?", function(data) { console.log(data); }); }); });

<script src="ajax.googleapis/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获取值

本文发布于:2023-10-12 02:54:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jQuery   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!