我正在尝试获取最近7天的天气信息,但它没有返回任何内容。当我从浏览器输入 url 时,它正在运行并返回xml
格式代码。但是当我用jquery代码运行我的html时,它没有执行。你能告诉我哪里出错了吗?
我的代码是:
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta charset="utf-8">
<title>OpenWeatherMap API jQuery Plugin</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<style>
/* for presentation only */
body {
font: 16px Arial, Helvetica, sans-serif;
margin: 0;
}
.weather-wrapper {
background: skyblue;
margin: 5% 0 5% 5%;
padding: 40px 5%;
float: left;
color: white;
width: 70%;
max-width: 400px;
}
strong {
color: steelblue;
}
.capitalize {
text-transform: capitalize;
}
.hide {
display: none;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="application/javascript">
$(document).ready(function(){
$.getJSON('http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa',function(result){
console.log(result);
});
});
</script>
</head>
<body lang="en">
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='js/lib/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
<script src="js/plugins/openWeather.min.js"></script>
</body>
</html>
答案 0 :(得分:2)
getJSON函数期望返回JSON数据。所以你需要一个可以返回XML数据的函数:
$.ajax({
type: "get",
url: "http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa",
dataType: "xml",
success: function(data) {
/* handle data here */
},
error: function(xhr, status) {
/* handle error here */
}
});
现在这个函数可以返回xml数据(dataType: "xml"
)
您还可以更改网址以返回JSON数据:
http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=json&appid=c9d49310f8023ee2617a7634de23c2aa
我在网址
中更改了mode=json