怎么通过 Javascript/JQuery/Ajax 来调用 API?


本站开始提供一些免费有意思的API让大伙来使用. 所有的API都是在这台主机VPS上实现和运行的.

举这个API为例 fortune 能随机的返回一段(句)话 然后我们就可以通过 Javascript/JQuery/Ajax 来动态的显示在浏览器中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script type='text/Javascript' src='https://helloacm.com/jquery/jquery-2.1.4.min.js'></script>
<script type='text/Javascript'>
$(document).ready(function () {
  $.ajax(
    {
        dataType: "json",
        url: "https://helloacm.com/api/fortune/", 
        cache: false, 
        success: function (response) {
          document.write(response);
        }
    })        
});                
</script>
<script type='text/Javascript' src='https://helloacm.com/jquery/jquery-2.1.4.min.js'></script>
<script type='text/Javascript'>
$(document).ready(function () {
  $.ajax(
    {
        dataType: "json",
        url: "https://helloacm.com/api/fortune/", 
        cache: false, 
        success: function (response) {
          document.write(response);
        }
    })        
});                
</script>

做少量修改加个显示 区域 和按钮

<div id='fortune_text'> </div>

并把 document.write 改成 $(‘#fortune_text’).html(response).



JQUERY 方便的调用AJAX并把获得的结果 按 JSON格式解码 要写的代码实际很少了.

英文: https://helloacm.com/how-to-invoke-apis-the-javascriptajax-example/

GD Star Rating
loading...
本文一共 132 个汉字, 你数一下对不对.
怎么通过 Javascript/JQuery/Ajax 来调用 API?. (AMP 移动加速版本)
上一篇: 在英国学车开车 - 全国速度 National Speed Limit
下一篇: 我的英文算法论坛 长草了

扫描二维码,分享本文到微信朋友圈
f3d5ecbe47d46a8c8955ea7d1f5562c0 怎么通过 Javascript/JQuery/Ajax 来调用 API? 互联网 技术 程序设计

评论