如何在Laravel中调用get请求API

jw5wzhpr  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(143)

我正在学习如何在Laravel中使用API,并在我的视图中显示json结果。这是我做的一个示例代码,但什么也没有显示。API是有价值的,因为当我在Postman中尝试它时,它返回json结果。

<html>
    <head>
        <meta charset="utf-8">
        <title>Item Manager</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>

我怀疑它可能有一个问题的key tags我试图调用。我试图作出的呼吁,从刀片不需要从控制器。
从API返回的json:

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

9gm1akwq1#

下面是完整的代码。

<html>
    <head>
        <meta charset="utf-8">
        <title>Item Manager</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>
mmvthczy

mmvthczy2#

尝试添加以下内容:

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

另外,console.log()的结果可能会有帮助。

相关问题