简体   繁体   English

如何使用 function 发出 api get 请求?

[英]How to make an api get request with a function?

I'm trying to generate a new, random dog image with the breed name underneath using calls to the dog.ceo api using Axios, sent out by the genNewImage() function linked to my button, this is my javascript:我正在尝试使用 Axios 调用 dog.ceo api 生成一个新的随机狗图像,该图像由链接到我的按钮的genNewImage() function 发送,这是我的 javascript:

 function genNewImage(){ axios.get('https://dog.ceo/api/breeds/image/random') //This is the api I'm calling to. .then((body) => { var url = body.data.message; var image = document.getElementById("dogImg"); var dogText = document.getElementById("dogText"); var urlCutter = url.split('/')[4].split('-'); //Get the breed name from the retrieved url var first = urlCutter[1].charAt(0).toUpperCase() + urlCutter[1].split("").slice(1,urlCutter[1].length).join(""); //Get the first part of the breed name & capitalize first letter. var second = urlCutter[0].charAt(0).toUpperCase() + urlCutter[0].split("").slice(1,urlCutter[0].length).join(""); //Get the second part of the breed name & capitalize first letter. image.src = url; // set the src of the image object dogText.innerHTML = "This is a: " + first + " " + second; //Set the dog breed name in the paragraph }).catch((err) => { console.log('Error', err.statusCode); }) };
 <button onClick="genNewImage()">Click for a random image</button> <img id="dogImg" src=""> <p><strong><span id ="dogText"> This is a: </span></strong></p>

I'm still really new to Axios and api calls in general.一般来说,我对 Axios 和 api 电话还是很陌生。 My function isn't working even though I've already used this code to make the api call on new page load and I don't understand why this one doesn't work.我的 function 不工作,即使我已经使用此代码在新页面加载时调用 api,我不明白为什么这个不工作。 Any advice would be greatly appreciated.任何建议将不胜感激。

The problem is not the code itself but a semantic problem.问题不在于代码本身,而是语义问题。

Consider these two example URLs考虑这两个示例 URL

https://images.dog.ceo/breeds/spaniel-cocker/n02102318_9714.jpg https://images.dog.ceo/breeds/spaniel-cocker/n02102318_9714.jpg

https://images.dog.ceo/breeds/chow/n02112137_6458.jpg https://images.dog.ceo/breeds/chow/n02112137_6458.jpg

Go trough your code line by line and you will see that the problem occurs when you try to get the first name of the breed. Go 逐行检查您的代码,您会发现当您尝试获取该品种的名字时会出现问题。 You access the splitted URL array at index 1. You split the array by '-' delimiter.您在索引 1 处访问拆分的 URL 数组。您用“-”分隔符拆分数组。 If the URL (breed) doesnt contain a '-' like the second URL does an error will be thrown because the array at index 1 is undefined.如果 URL(品种)不像第二个 URL 那样包含“-”,则会抛出错误,因为索引 1 处的数组未定义。 So before using the value at index 1 check if it is set at all.因此,在使用索引 1 处的值之前,请检查它是否已设置。 This will make your code run.这将使您的代码运行。

The error message also indicates that a variable is undefined.该错误消息还表明变量未定义。

 function genNewImage() { axios.get('https://dog.ceo/api/breeds/image/random').then((body) => { let url = body.data.message; let image = document.getElementById("dogImg"); let dogText = document.getElementById("dogText"); let urlCutter = url.split('/')[4].split('-'); let first = typeof(urlCutter[1]) == 'undefined'? "": urlCutter[1].charAt(0).toUpperCase() + urlCutter[1].split("").slice(1, urlCutter[1].length).join("");; let second = urlCutter[0].charAt(0).toUpperCase() + urlCutter[0].split("").slice(1, urlCutter[0].length).join(""); image.src = url; // set the src of the image object dogText.innerHTML = "This is a: " + first + " " + second; //Set the dog breed name in the paragraph }).catch(error => { console.log(error.response) }); }
 <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <button onClick="genNewImage()">Click for a random image</button> <img id="dogImg" src=""> <p><strong><span id ="dogText"> This is a: </span></strong></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM