gpt4 book ai didi

javascript - 使用 javascript 发出 json 请求

转载 作者:行者123 更新时间:2023-12-02 19:25:41 29 4
gpt4 key购买 nike

我正在制作一个使用 API 的网络应用程序。问题是我从来没有用 javascript 做过 JSON 请求(我曾经使用 python)。这是我的代码:

<head>
<title>IP finder pro</title>
<script>
function find_ip() {
var ip = document.getElementById('ip_input').value;
fetch('http://ipapi.co/8.8.8.8/json')
.then(response => response.json())
.then(data => alert(data));
}
</script>
</head>
<body>
<input id="ip_input">
<button id="ip_button" onclick="find_ip()">Click !</button>
</body>

这是 JSON 文件:

{
"ip": "8.8.8.8",
"city": "Mountain View",
"region": "California",
"region_code": "CA",
"country": "US",
"country_code": "US",
"country_code_iso3": "USA",
"country_capital": "Washington",
"country_tld": ".us",
"country_name": "United States",
"continent_code": "NA",
"in_eu": false,
"postal": "Sign up to access",
"latitude": "Sign up to access",
"longitude": "Sign up to access",
"timezone": "America/Los_Angeles",
"utc_offset": "-0700",
"country_calling_code": "+1",
"currency": "USD",
"currency_name": "Dollar",
"languages": "en-US,es-US,haw,fr",
"country_area": 9629091.0,
"country_population": 310232863.0,
"message": "Please message us at ipapi.co/trial for full access",
"asn": "AS15169",
"org": "GOOGLE"
}

这是curl命令:

curl 'https://ipapi.co/8.8.8.8/json/'

你知道我该怎么做吗?

最佳答案

您可以使用 fetch API 来实现此目的并解码您的 JSON。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));

关于javascript - 使用 javascript 发出 json 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62425240/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com