簡體   English   中英

如何使用 AJAX 和 Axios 從 API 中選擇某些數據?

[英]How do I select certain data from API using AJAX and Axios?

我想知道在使用 Axios 從 API 中選擇 URL(或任何數據段)時我做錯了什么。 舉個例子,我想選擇網址: “https://media4.giphy.com/media/yy6hXyy2DsM5W/giphy-downsized.gif?cid=482277c2s3j1s8uell6vhu03111i46npv57g8yqgikgiefr8&ridgiphy=482277c2s3j1s8uell6vhu03111i46npv57g8yqgikgiefr8&ridgiphy

來自 API 鏈接: https : //api.giphy.com/v1/gifs/search?q=$puppies&api_key=MhAodEJIJxQMxW9XqxKjyXfNYdLoOIym

我的 HTML 和 Javascript 包括在內。 我的函數正在獲取所有 API 數據,但似乎我的res.data[0].images.downsized.url沒有准確寫入。 任何幫助將非常感激!

 async function getGiphs() { const res = await axios.get(`https://api.giphy.com/v1/gifs/search?q=$puppies&api_key=MhAodEJIJxQMxW9XqxKjyXfNYdLoOIym`); console.log(res.data[0].images.downsized.url); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Giphy Party!</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> </head> <body> <div class="jumbotron"> <h1 class="display-4 mb-3">GIPHY Party!</h1> <div class="form-group"> <form> <input id="search-word" class="form-control mb-3" placeholder="Enter search term"> <button id="search-btn" class="btn btn-primary btn-md mr-2" role="button" type="submit">Search GIPHY <button id="remove-btn" class="btn btn-danger btn-md" role="button">Remove Images </form> </div> <script src="giphyParty.css"></script> <script src="https://code.jquery.com/jquery-3.2.1.js"></script> <script src="https://unpkg.com/axios/dist/axios.js"></script> <script src="giphyParty.js"></script> </body> </html>

 /* i dont know see where and how you'r calling 'getGiphs' method. there is a small correction in accessing the results. */ async function getGiphs() { const res = await axios.get(`https://api.giphy.com/v1/gifs/search?q=$puppies&api_key=MhAodEJIJxQMxW9XqxKjyXfNYdLoOIym`); console.log(res.data.data[0].images.downsized.url); }

您只需要再添加一個“數據”,因為返回的對象也有一個名為“數據”的屬性。

console.log(res.data.data[0].images.downsized.url)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM