我试图将变量 lat
和 long
放入字符串 url 中,但它不起作用。这是我的代码:
$(document).ready(function() {
var long;
var lat;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
long = position.coords.longitude;
var api = "http://api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+long+'&appid=c873adc18574701f4fb0abe01d927819";
$.getJSON(api, function(data) {
alert(data.name);
});
});
}
});
最佳答案
您需要使用相同类型的引号来关闭和重新打开字符串。
var api= "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&appid=c873adc18574701f4fb0abe01d927819";
关于javascript - 在Javascript中将变量插入另一个字符串变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409833/