使用字段来完成API调用(Dark Sky Forecast API)

Using fields to complete an API call (Dark Sky Forecast API)

本文关键字:API Dark Sky Forecast 调用 字段      更新时间:2023-09-26

所以我一直在玩弄黑暗天空预报API,我能够得到我自己的位置的天气显示在一个网页上。然而,我硬编码了经度和纬度,我想更进一步,让用户输入他们自己的。这是它停止工作的地方

之前我的代码只是简单的

                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
                <script>
                //Waits until document is ready to run
                jQuery(document).ready(function($) {
                    //Beginning of code
                    //Makes the request
                    $.ajax({
                    url : "https://api.darksky.net/forecast/MyKey/1111,-1111+?exclude=minutely,hourly,daily,alerts,flags",
                    dataType : "jsonp",
                    success : function(parsed_json) {
                    var location = parsed_json['timezone'];
                    var temp_f = parsed_json['currently']['temperature'];
                    var complete = ("Current temperature in " + location + " is: " + temp_f);
                    $('#random').html("<h1>" + complete + "</h1>")
                    }
                    });
                    }
                });
                </script>
                    $('#search').click(getInfo);
                });
                </script>

然后我创建了一些HTML字段(我使用bootstrap只是为了保持整洁)像这样

                        <div class="input-group input-group-lg">
                        <input id="long" type="text" class="form-control" placeholder="Longitude" aria-describedby="sizing-addon1">
                    </div>
                    <div class="input-group input-group-lg">
                        <input id="lat" type="text" class="form-control" placeholder="Latitude" aria-describedby="sizing-addon1">
                    </div>
                    <div class="rand">
                    <button class="btn btn-primary btn-lg" role="button" id="search">Find my location and temperature!</button>
                    </div>

我更新了我的脚本,看起来像这样

                     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
                <script>
                //Waits until document is ready to run
                jQuery(document).ready(function($) {
                    //Beginning of code
                    var getInfo = function(){
                        //Grabs the longitude and latitude
                        var Longitude = $('#long').val();
                        var Latitude = $('#lat').val();
                        //Makes the request
                        $.ajax({
                        url : "https://api.darksky.net/forecast/mykey/" + Longitude + ","+ Latitude + "+?exclude=minutely,hourly,daily,alerts,flags",
                        dataType : "jsonp",
                        success : function(parsed_json) {
                        var location = parsed_json['timezone'];
                        var temp_f = parsed_json['currently']['temperature'];
                        var complete = ("Current temperature in " + location + " is: " + temp_f);
                        $('#random').html("<h1>" + complete + "</h1>")
                        }
                        });
                    }
                    $('#search').click(getInfo);
                });

然而,在这一点上,它只是不起作用。我点了按钮,什么也没发生。也许我在HTML部分做错了,因为我几乎没有对脚本做任何改变,所以我猜这不是问题。

非常感谢!

算了,我明白了。我做的一切都是正确的,我不小心在url中放了一个加号…哎呀。