【问题标题】:Javascript doesn't seem to be processing JSON apiJavascript 似乎没有处理 JSON api
【发布时间】:2016-09-02 22:02:28
【问题描述】:

我正在尝试创建一个页面,该页面将使用 openweathermap api 来显示用户的城市和当地温度,不幸的是它似乎没有处理 JSON api 并且什么也没做。据我所知,我的代码很好,所以我不明白出了什么问题。
这是 jsfiddle 链接和 javscript 代码:
https://jsfiddle.net/qo2h1L9e/2/

 $(document).ready(function() {
  var data;
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var lat = position.coords.latitude;
      var lon = position.coords.longitude;
      console.log(lat);
      console.log(lon);
      console.log("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886");
      $.getJSON("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886", function(json) {
        data = json;
        console.log(data.name);
        $("#city").text(data.name);
      });
    });
  }
});

【问题讨论】:

  • 地址被假定为相对于当前的window.locationapi.openweathermap.org 也可以是一个有效的目录名称。在地址中包含协议和 // 锚点,使其被识别为主机名 - "http://api.openweathermap.org/..."
  • 我按照你的建议做了,但没有帮助。当我从控制台获取 url 并将其放入搜索引擎的地址栏中时,我在页面上获得了所需的 JSON 对象,但由于某种原因我的代码似乎没有收到它。
  • 您是否收到任何错误消息?浏览器通常会在其控制台中记录 Ajax 请求失败的原因。
  • 之前它给了我一个错误 404,但不知何故(我没有改变任何东西)它才开始工作。我对此比以前更加困惑,但我不会抱怨。
  • 也许您的建议有延迟效果? :)

标签: javascript jquery json api openweathermap


【解决方案1】:

我已经完成了 jQuery 试试这段代码

        $(document).ready(function() {
            var data;
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    var lat = position.coords.latitude;
                    var lon = position.coords.longitude;
                    console.log(lat);
                    console.log(lon);
                    console.log("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886");
//                    $.getJSON("api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=4907e373098f091c293b36b92d8f0886", function(json) {
//                        data = json;
//                        console.log(data.name);
//                        $("#city").text(data.name);
                    //                    });
                    var url = 'http://api.openweathermap.org/data/2.5/weather';
                    var params = {};
                    params.lat = lat;
                    params.lon = lon;
                    params.APPID = "4907e373098f091c293b36b92d8f0886";

                    $.ajax({
                        type: "GET",
                        url: url,
                        dataType: "jsonp",
                        contentType: "application/json; charset=utf-8",
                        data: params,
                        success: function (res) {
                            console.log(res);
                        },
                        error: function (res) {
                        }
                    });


                }
                );
            }
        });

问题出在json。跨域使用jsonp

【讨论】:

    【解决方案2】:

    我在编码时遇到了类似的问题。假设你在 FCC?

    无论如何,尝试添加 &callback=?到 api URL。您可能需要以 JSONP 而不是 JSON 格式获取数据。

    另外,您不需要定义数据。您可以通过 json 参数直接访问该对象。

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多