用api写一个简单的天气查询页面

如下代码可以直接复制运行,所有库和框架的链接都是基于百度静态公共库的。
api文档:http://openweathermap.org/cur...
想请教下各位大神,为什么一直报错。。

<!DOCTYPE html>
<html>
  <head>
     <meta charset = "utf-8">
     <meta name = "viewport" content = "width = device-width,initial-scale = 1,user-scalable = 0">
     <title>bootstrap</title>
     <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
     <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
     <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    
     <style>
        body {}
        
     </style>
  </head>
  <body>
    <div >
       <input type  = "text" placeholder="请输入地区zip-code" ><button id = "search">查询</button>
       <div class = "info_weather">
            <!-- <input type = "text" class = "loc"> -->
            <div class="input-group">
                  <span class="input-group-addon" id="sizing-addon2">地区</span>
                  <input class = "loc" type="text" class="form-control" placeholder="Username" aria-describedby="sizing-addon2">
            </div>
            <!-- <input type = "text" class = "temp"> -->
            <div class="input-group">
                  <span class="input-group-addon" id="sizing-addon2">温度</span>
                  <input class = "temp" type="text" class="form-control" placeholder="Username" aria-describedby="sizing-addon2">
            </div>
            
       </div>
    </div> 
     <script>
        $("#search").click(function(){
            var input_value = $("input").val();
            $.ajax({ 
              type:"GET",
              url: "http://api.openweathermap.org/data/2.5/weather?{zip =input_value }",
              data: "appid=ab21b8f814629480b964369f72fb2d70",
             // data: "zip=94040,us",
              dataType:"json",         
              success: function(meg){
                   $(".loc").val() = "经度"+meg["coord"].lon+"纬度"+meg["coord"].lat;
                   $(".temp").val() = meg["main"].temp;
          }});
     });
     </script> 
       
  </body>

</html>






































































































































































































































































































































































阅读 3.4k
2 个回答

刚入门jquery?

$("#search").click(function(){
    var input_value = $("input").val();
    $.ajax({
        type:"GET",
        url: "http://api.openweathermap.org/data/2.5/weather?zip="+input_value,
        data: "appid=ab21b8f814629480b964369f72fb2d70",
        // data: "zip=94040,us",
        dataType:"json",
        success: function(meg){
            $(".loc").val("经度"+meg["coord"].lon+"纬度"+meg["coord"].lat);
            $(".temp").val(meg["main"].temp);
        }});
});
        $("#search").click(function(){
            var input_value = $("input").val();
            $.ajax({
                type:"GET",
                url: "http://api.openweathermap.org/data/2.5/weather",
                data: {appid:'ab21b8f814629480b964369f72fb2d70', zip:input_value},
                // data: "zip=94040,us",
                dataType:"json",
                success: function(meg){
                    $(".loc").val("经度"+meg["coord"].lon+"纬度"+meg["coord"].lat);
                    $(".temp").val(meg["main"].temp);
                }});
        });

输入 90001

返回

{
    "coord": {
        "lon": -118.24, 
        "lat": 33.97
    }, 
    "weather": [
        {
            "id": 701, 
            "main": "Mist", 
            "description": "mist", 
            "icon": "50n"
        }
    ], 
    "base": "stations", 
    "main": {
        "temp": 282.06, 
        "pressure": 1013, 
        "humidity": 53, 
        "temp_min": 279.15, 
        "temp_max": 284.15
    }, 
    "visibility": 16093, 
    "wind": {
        "speed": 1.05, 
        "deg": 0.501343
    }, 
    "clouds": {
        "all": 1
    }, 
    "dt": 1488283260, 
    "sys": {
        "type": 1, 
        "id": 396, 
        "message": 0.0122, 
        "country": "US", 
        "sunrise": 1488291704, 
        "sunset": 1488332957
    }, 
    "id": 7261268, 
    "name": "Florence-Graham", 
    "cod": 200
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题