首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Javascript从openweathermap API检索5天天气海报

使用Javascript从openweathermap API检索5天天气海报
EN

Stack Overflow用户
提问于 2016-10-31 01:51:44
回答 2查看 979关注 0票数 1

我得到一个404错误。我只想检索5天的天气信息使用open扳机地图api (严格的javascript,没有jquery。我正在练习api和javascript,以及使用一个电影api的相同结构。我犯的错误是什么?下面是目前为止我的代码

ps:预览时缩小到25%

代码语言:javascript
复制
function enterkey(){
	
	var enterk = event.which || event.keyCode;
	
	if(enterk==13){
		
		showheather();
	}
	
}

function 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 = function(){
        
        if(http.status==200 && http.readyState==4){
            
            var wdata =JSON.parse(http.responseText)
			
			
			
			wgoes.innerHTML = wdata.city.name
			
			
			
		}
		
		
		
		
		
	}
	
	
}
代码语言:javascript
复制
#wraper{
margin: 0 auto;
height: 4000px;
width:3840px;

}
#posterdiv{
margin-top: 0%;
height: 2345px;
width:3840px;
border: 5px solid blue;
border-radius: 15px;
}

#searchbox{
height: 300px;
width:3840px;
font-size: 250px;
letter-spacing: 50px;
border-style: none;
	
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="../css/weather.css">
	
  <title></title>
</head>
<body>
  
<div id="wraper">
	
	
	<input type="text" id="searchbox" placeholder="Type City Name Here.." onkeydown="enterkey()" >
	
	<div id="posterdiv"></div>
	
	
	
	
	
	
</div>	
	
	
	
	
	
<script src = "../JavaScript/weather.js"></script>
	
</body>
</html>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-31 15:20:32

代码语言:javascript
复制
function enterkey(){
	
	var enterk = event.which || event.keyCode;
	
	if(enterk==13){
		
		showheather();
	}
	
}

function 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 = function(){
        
        if(http.status==200 && http.readyState==4){
            
            var wdata =JSON.parse(http.responseText)
			
			
			
			wgoes.innerHTML = wdata.city.name
			
			
			
		}
		
		
		
		
		
	}
	
	
}

api密钥是问题所在。openweathermap要求用户拥有api密钥。不仅仅是json链接。现在还可以添加像魅力一样的http .works

票数 0
EN

Stack Overflow用户

发布于 2016-10-31 02:05:41

您需要通过urlapi传递一个完整的绝对路径。

现在它是一个相对路径,所以您将API调用发送到您自己的主机。

opeanweathermap还需要一个API键,您需要在url中传递它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40335599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档