【问题标题】:Pulling Information from Open Weather API从开放天气 API 中提取信息
【发布时间】:2017-10-04 18:58:59
【问题描述】:

我正在Code Pen 上编写一个网络应用程序,它获取用户位置并显示当前当地天气。经纬度拉取成功,但是API调用中好像拉不到我需要的JSON信息。

我正在用 AJAX 解析它,我相信我已经完成了Official API Documentation 的要求,但它不起作用,数据甚至没有显示在 控制台 上。我已经查看了很多答案,但无法正常工作。

我想在 blockquote

中添加信息

if (navigator.geolocation) {
    //Return the user's longitude and latitude on page load using HTML5 geolocation API
    window.onload = function () {
    function getCurrentLocation (position) {
        
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
      
      console.log(latitude);
      console.log(longitude);

      $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=b7aaa3a349294d5706002e82df3de1ea&units=metric", function (data) {
        console.log(data);
        console.log(weather.main.temp);
        $(".city").append(name + " ");
        $(".temperature").append(temp + " ");
        $(".weatherdescription").append(field + " ");
            })
          
        };
    }
   
      navigator.geolocation.getCurrentPosition(getCurrentLocation);
    };
 
<h1 class="jumbotron" style="color:white"><em>The Weather Today</em></h1>

<div class="container-fluid">
  <div class = "row text-center">
  </div>
  <div id="weather" class = "row text-center">
    <div class = "col-xs-12 well message">
      <blockquote id = "raw_json">
        <div class="weatherbox">

      <strong class="city">{CITY NAME HERE}</strong>
    <br/>
      <span class="temperature">{X} °C</span>
    <br/>
      <span class="weatherdescription">{WEATHER DESCRIPTION HERE}</span>
        <br/>

    </div>
    </blockquote>
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      
    </div>
  </div>
</div>

【问题讨论】:

    标签: javascript jquery json openweathermap codepen


    【解决方案1】:

    用下面的代码替换你的javascript代码,它应该可以工作。

    if (navigator.geolocation) {
        //Return the user's longitude and latitude on page load using HTML5 geolocation API
        window.onload = function () {
            navigator.geolocation.getCurrentPosition(getCurrentLocation);
    
        }
    
    }
    
    
    function getCurrentLocation(position) {
    
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
    
        console.log(latitude);
        console.log(longitude);
    
        $.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=b7aaa3a349294d5706002e82df3de1ea&units=metric", function (data) {
            console.log(data);
            console.log(weather.main.temp);
            $(".city").append(name + " ");
            $(".temperature").append(temp + " ");
            $(".weatherdescription").append(field + " ");
        })
    
    };
    

    CODEPEN: https://codepen.io/azimjs/pen/KXybpa?editors=0011

    【讨论】:

    • 这似乎解决了坐标问题,谢谢!但是 API 调用仍然是空的...
    • 存在语法错误。我已经更新了代码笔。如果可以解决您的问题,请将其标记为已接受的答案。谢谢!
    • 这个成功了,谢谢!你能详细说明我犯了哪些语法错误吗?
    • 1.您没有使用“数据”对象来获取 api 结果。 2. jquery 类选择器 $(".className") 返回一个数组,因此您应该使用数组位置 $(".className")[#] 来访问它。谢谢!
    猜你喜欢
    • 2013-01-15
    • 2015-02-22
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多