我收到404错误。我只想使用openwheathermap api检索5天的天气信息(严格来说是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>
&#13;
答案 0 :(得分:0)
您需要通过urlapi
现在它是一个相对路径,因此您将API调用发送到您自己的主机。
opeanweathermap还需要一个API密钥,您需要在该网址中传递。
答案 1 :(得分:0)
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
}
}
}
&#13;
api键是问题所在。 openweathermap要求用户拥有api密钥。不仅仅是json链接。还可以像现在的魅力一样添加http .works