如何在Laravel中进行GET请求API调用

huangapple go评论64阅读模式
英文:

How to make a get request api call in Laravel

问题

这是您提供的代码的翻译部分:

<html>
<head>
    <meta charset="utf-8">
    <title>项目管理器</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>

<div class="container">
    <ul id="items" class="list-group">
    </ul>
</div>

<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>

<script type="text/javascript">
    $(document).ready(function (){
        getItems();

       function getItems(){
           $.ajax({
               url:'https://www.boredapi.com/api/activity'
           }).done(function (items){
            let output = '';
            $.each(items, function (key, activity){
                output += '<li class="list-group-item">' +
                    '<strong>${activity.text}</strong>${text.body}' +
                    '</li>';
            });
           });
       }
    });
</script>
</body>
</html>

您猜测可能与您尝试调用的“键标签”有问题。我正在尝试从 Blade 调用,而不是从控制器中获取数据。

从API返回的JSON数据如下:

{
    "activity": "写下你感到感激的事物清单",
    "type": "放松",
    "participants": 1,
    "price": 0,
    "link": "",
    "key": "2062010",
    "accessibility": 0
}
英文:

I am trying to learn how to use an API in Laravel and show json results in my view. This is a sample code I have made but nothing is showing. The API is with values because when I try it in Postman, it returns json results.

    &lt;html&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot;&gt;
        &lt;title&gt;Item Manager&lt;/title&gt;
        &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
    
    &lt;div class=&quot;container&quot;&gt;
        &lt;ul id=&quot;items&quot; class=&quot;list-group&quot;&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    
    &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.min.js&quot; integrity=&quot;sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    
    &lt;script type=&quot;text/javascript&quot;&gt;
        $(document).ready(function (){
            getItems();
    
           function getItems(){
               $.ajax({
                   url:&#39;https://www.boredapi.com/api/activity&#39;
               }).done(function (items){
                let output = &#39;&#39;;
                $.each(items, function (key, activity){
                    output += &#39;&lt;li class=&quot;list-group-item&quot;&gt;&#39; +
                        &#39;&lt;strong&gt;${activity.text}&lt;/strong&gt;${text.body}&#39; +
                        &#39;&lt;/li&gt;&#39;
                });
               });
           }
        });
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;

I suspect it might have a problem with the key tags i am trying to call. I am trying to make the call from blade not needed from the controller.

json returned from the API:

{
    &quot;activity&quot;: &quot;Write a list of things you are grateful for&quot;,
    &quot;type&quot;: &quot;relaxation&quot;,
    &quot;participants&quot;: 1,
    &quot;price&quot;: 0,
    &quot;link&quot;: &quot;&quot;,
    &quot;key&quot;: &quot;2062010&quot;,
    &quot;accessibility&quot;: 0
}

答案1

得分: 1

以下是代码的中文翻译部分:

这是完成的代码。

<!-- 开始代码段:JavaScript 隐藏:false 控制台:true Babel:false -->

<!-- 语言:HTML -->

     <html>
        <head>
            <meta charset="utf-8">
            <title>项目管理器</title>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
        </head>
        <body>
        
        <div class="container">
            <ul id="items" class="list-group">
            </ul>
        </div>
        
        <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
        
        <script type="text/javascript">
            $(document).ready(function (){
                getItems();
                var output = '';
               function getItems(){
                   $.ajax({
                       url:'https://www.boredapi.com/api/activity'
                   }).done(function (items){
                    let output = '';
                    $.each(items, function (key, activity){
                        output += '<li class="list-group-item">' +
                            '<strong>'+key+'</strong>' + activity +
                            '</li>'
                    });
                          $("#items").append(output);

                   });
               }
            });
        </script>
        </body>
        </html>

请注意,代码中的HTML和JavaScript部分已经被翻译成中文。

英文:

Here is the completed code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

 &lt;html&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;utf-8&quot;&gt;
        &lt;title&gt;Item Manager&lt;/title&gt;
        &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
    
    &lt;div class=&quot;container&quot;&gt;
        &lt;ul id=&quot;items&quot; class=&quot;list-group&quot;&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    
    &lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.min.js&quot; integrity=&quot;sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    
    &lt;script type=&quot;text/javascript&quot;&gt;
        $(document).ready(function (){
            getItems();
    		var output = &#39;&#39;;
           function getItems(){
               $.ajax({
                   url:&#39;https://www.boredapi.com/api/activity&#39;
               }).done(function (items){
                let output = &#39;&#39;;
                $.each(items, function (key, activity){
                    output += &#39;&lt;li class=&quot;list-group-item&quot;&gt;&#39; +
                        &#39;&lt;strong&gt;&#39;+key+&#39;&lt;/strong&gt;&#39; + activity +
                        &#39;&lt;/li&gt;&#39;
                });
                      $(&quot;#items&quot;).append(output);

               });
           }
        });
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

尝试添加以下代码:

$.ajax({
    url: 'https://www.boredapi.com/api/activity',
    type: "GET",
});

还可以使用 console.log() 打印结果,可能会有帮助。

英文:

try adding this:

$.ajax({
url:&#39;https://www.boredapi.com/api/activity&#39;,
type: &quot;GET&quot;,
},

Also console.log() your result might be helpful.

huangapple
  • 本文由 发表于 2023年2月16日 18:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定