使用Javascript从OpenWeatherMapAPI检索5天天气海报
我得到一个404错误。我只想使用openwheathermap api(严格来说是javascript,没有jquery)检索5天的天气信息。我正在练习api和javascript,并且使用与电影api相同的结构。我犯了什么错误。下面是我到目前为止的代码 ps:预览时缩小到25%使用Javascript从OpenWeatherMapAPI检索5天天气海报,javascript,api,openweathermap,Javascript,Api,Openweathermap,我得到一个404错误。我只想使用openwheathermap api(严格来说是javascript,没有jquery)检索5天的天气信息。我正在练习api和javascript,并且使用与电影api相同的结构。我犯了什么错误。下面是我到目前为止的代码 ps:预览时缩小到25% 函数enterkey(){ var enterk=event.which | | event.keyCode; 如果(输入=13){ showheather(); } } 函数showheather(){ var c
函数enterkey(){
var enterk=event.which | | event.keyCode;
如果(输入=13){
showheather();
}
}
函数showheather(){
var cName=document.getElementById(“searchbox”).value;
var wgoes=document.getElementById(“posterdiv”);
var urlapi=“api.openweathermap.org/data/2.5/weather?q=“+cName+”;
var http=new XMLHttpRequest();
http.open(“GET”、urlapi、true)
http.send()
http.onreadystatechange=函数(){
if(http.status==200&&http.readyState==4){
var wdata=JSON.parse(http.responseText)
wgoes.innerHTML=wdata.city.name
}
}
}
#包装机{
保证金:0自动;
高度:4000px;
宽度:3840px;
}
#波斯特尔迪夫{
利润率最高:0%;
高度:2345px;
宽度:3840px;
边框:5px纯蓝色;
边界半径:15px;
}
#搜索框{
高度:300px;
宽度:3840px;
字体大小:250px;
字母间距:50px;
边框样式:无;
}
您需要通过
urlapi
现在它是一个相对路径,所以您要将API调用发送到您自己的主机
opeanweathermap还需要API密钥,您需要在url中传递该密钥。您需要通过
urlapi
现在它是一个相对路径,所以您要将API调用发送到您自己的主机
opeanweathermap还需要API密钥,您需要在url中传递该密钥。
函数enterkey(){
var enterk=event.which | | event.keyCode;
如果(输入=13){
showheather();
}
}
函数showheather(){
var cName=document.getElementById(“searchbox”).value;
var wgoes=document.getElementById(“posterdiv”);
var urlapi=”http://api.openweathermap.org/data/2.5/weather?q=“+cName+”&appid='enter your app id';
var http=new XMLHttpRequest();
http.open(“GET”、urlapi、true)
http.send()
http.onreadystatechange=函数(){
if(http.status==200&&http.readyState==4){
var wdata=JSON.parse(http.responseText)
wgoes.innerHTML=wdata.city.name
}
}
}
函数enterkey(){
var enterk=event.which | | event.keyCode;
如果(输入=13){
showheather();
}
}
函数showheather(){
var cName=document.getElementById(“searchbox”).value;
var wgoes=document.getElementById(“posterdiv”);
var urlapi=”http://api.openweathermap.org/data/2.5/weather?q=“+cName+”&appid='enter your app id';
var http=new XMLHttpRequest();
http.open(“GET”、urlapi、true)
http.send()
http.onreadystatechange=函数(){
if(http.status==200&&http.readyState==4){
var wdata=JSON.parse(http.responseText)
wgoes.innerHTML=wdata.city.name
}
}
}
urlapi
需要以http://
或https://
甚至//
开始-事实上,您的代码正在尝试从承载页面的主机读取我在无工作之前尝试了http://当您尝试http://
时,编辑不需要http://
,您尝试了哪些信息你可以进入开发者工具控制台和网络选项卡,帮助我们确定你做错了什么。你是在chrum、IE还是edge上运行的?因为你的代码不会在这些劣等浏览器之外运行,它们的非标准全局事件对象是你引用的urlapi
需要从http:///c开始的ode>或https://
或甚至//
-事实上,您的代码正在尝试从承载页面的主机读取。我在“无工作”之前尝试了http//,编辑需要http://
而不是http//
当您尝试http://
时,您在开发人员工具控制台和网络选项卡中获得了哪些可能会影响您的信息请帮助我们确定您做错了什么您是在chrum、IE还是edge上运行此程序?因为您的代码不会在这些劣等浏览器之外运行,因为它们的非标准全局事件对象也是您在url中引用的应用程序密钥。请在url中传递应用程序密钥。