我得到一个404错误。我只想检索5天的天气信息使用open扳机地图api (严格的javascript,没有jquery。我正在练习api和javascript,以及使用一个电影api的相同结构。我犯的错误是什么?下面是目前为止我的代码
ps:预览时缩小到25%
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
}
}
}#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;
}<!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>
发布于 2016-10-31 15:20:32
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
发布于 2016-10-31 02:05:41
您需要通过urlapi传递一个完整的绝对路径。
现在它是一个相对路径,所以您将API调用发送到您自己的主机。
opeanweathermap还需要一个API键,您需要在url中传递它。
https://stackoverflow.com/questions/40335599
复制相似问题