Javascript 使用fetch api传递url变量
我试图通过api获取传递url变量,但无法返回任何结果。谢谢,我是Javascript的新手Javascript 使用fetch api传递url变量,javascript,json,Javascript,Json,我试图通过api获取传递url变量,但无法返回任何结果。谢谢,我是Javascript的新手 //Get IP address fetch('https://extreme-ip-lookup.com/json/') .then((eip) => { return eip.json(); }).then((eip) => { document.getElementById('ip').value = eip.query; var myip = docu
//Get IP address
fetch('https://extreme-ip-lookup.com/json/')
.then((eip) => {
return eip.json();
}).then((eip) => {
document.getElementById('ip').value = eip.query;
var myip = document.getElementById('ip').value;
var url = "https://api.pray.zone/v2/times/today.json?ip=" + myip;
})
//Get City
fetch(url)
.then((res) => {
return res.json();
}).then((res) => {
document.getElementById('city').value = res.results.location.city;
})
我可以获取ip地址,但不能获取城市。
url
仅在中可见。然后回调,当您第二次调用fetch
时,它甚至不存在
在那里调用第二个fetch
,并返回fetch
返回的承诺,以便您可以正确地链接它们:
//Get IP address
fetch('https://extreme-ip-lookup.com/json/')
.then((eip) => {
return eip.json();
})
.then((eip) => {
document.getElementById('ip').value = eip.query;
var myip = document.getElementById('ip').value;
return fetch("https://api.pray.zone/v2/times/today.json?ip=" + myip);
})
//Get City
.then((res) => {
return res.json();
})
.then((res) => {
document.getElementById('city').value = res.results.location.city;
})
相关:url
仅在中可见。然后回调,当您第二次调用fetch
时,url甚至不存在
在那里调用第二个fetch
,并返回fetch
返回的承诺,以便您可以正确地链接它们:
//Get IP address
fetch('https://extreme-ip-lookup.com/json/')
.then((eip) => {
return eip.json();
})
.then((eip) => {
document.getElementById('ip').value = eip.query;
var myip = document.getElementById('ip').value;
return fetch("https://api.pray.zone/v2/times/today.json?ip=" + myip);
})
//Get City
.then((res) => {
return res.json();
})
.then((res) => {
document.getElementById('city').value = res.results.location.city;
})
相关:您的代码以异步方式运行,并且在获取url之前调用获取
//Get IP address
fetch('https://extreme-ip-lookup.com/json/')
.then((eip) => {
return eip.json();
}).then((eip) => {
document.getElementById('ip').value = eip.query;
var myip = document.getElementById('ip').value;
var url = "https://api.pray.zone/v2/times/today.json?ip=" + myip;
fetch(url)
.then((res) => {
return res.json();
}).then((res) => {
document.getElementById('city').value = res.results.location.city;
})
});
您的代码以异步方式运行,并且在获取url之前正在调用fetch
//Get IP address
fetch('https://extreme-ip-lookup.com/json/')
.then((eip) => {
return eip.json();
}).then((eip) => {
document.getElementById('ip').value = eip.query;
var myip = document.getElementById('ip').value;
var url = "https://api.pray.zone/v2/times/today.json?ip=" + myip;
fetch(url)
.then((res) => {
return res.json();
}).then((res) => {
document.getElementById('city').value = res.results.location.city;
})
});
url变量不在范围内,因此实际上您正在将未定义的内容传递到第二个fetch。。。您可以很容易地将第二次获取放在第一次获取关闭之前,或者在第一次获取外部定义url变量,并可能将其分配给空字符串,然后在第一次获取关闭之前构建url值或url后分配url值。这就是如果您想保持fetch API的saparated与您现在的方式相同url变量不在范围内,因此实际上您正在将未定义的传递到第二个fetch中。。。您可以很容易地将第二次获取放在第一次获取关闭之前,或者在第一次获取外部定义url变量,并可能将其分配给空字符串,然后在第一次获取关闭之前构建url值或url后分配url值。这是如果您希望保持fetch API保持现在的状态fetch
是一个承诺,因此您应该能够在方法中返回url值,然后
。或者将后面的第二个fetch
移动到url分配中fetch
的可能重复是一个承诺,因此您应该能够在方法中返回url值,然后。或者将第二个fetch
移动到url分配的后面,这很好!它工作得很好。你说得对,它在“.then”回叫中。太好了!它工作得很好。你说得对,它在“.then”回调中。