【发布时间】:2017-05-05 02:52:39
【问题描述】:
我有 2 个部分
- 第一部分,我想从用户那里获取位置输入。这是 .getJSON 不执行的地方。
- 第二部分,我检查地理定位是否与浏览器兼容,如果允许,则执行 .getJSON。这个很好用。
我认为if(navigator.geolocation) 不会检查是否已授予权限,只会检查地理位置是否与浏览器兼容。
第 1 节 $.getJSON(address, function(data) 中的这一行不像第 2 节那样执行。如果我将 `console.log(address) 放在它之前,它会显示,但在 .getJSON 范围内它不会。
因为功能相同,所以对正在发生的事情感到困惑。
$(document).ready(function() {
//GET STUFF FUNCTION
function getStuff(address) {
api = address; //**EDIT** changed to address
console.log("clicking or entering works yes");
$.getJSON(api, function(data) {
console.log("yay it works");
});
};
// SECTION 1
$("#searchForm").submit(function(e) {
var searchInput = $("#searchInput").val();
var address = "api.openweathermap.org/data/2.5/weather?q=" + searchInput + "&appid=?";
e.preventDefault();
getStuff(address);
});
$("#searchInput").keypress(function(e) {
if (e.which == 13) {
$("#searchBtn").click();
};
});
//SECTION 2
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
var address = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&appid=?';
//tried to use this function for search
getStuff(address);
});
我尝试尽我所能缩小这个,我不确定如何在小提琴上执行 AJAX 请求。 编辑:更新https://jsfiddle.net/mq10ps5c/10/
谢谢大家,任何批评都非常感谢
【问题讨论】:
-
为什么一个 URL 有 http:// 而另一个没有?
-
谢谢谢谢!这就是问题所在,我很尴尬,但谢谢你
标签: javascript jquery json navigator