javascript - 在Javascript中将变量插入另一个字符串变量中

标签 javascript

我试图将变量 latlong 放入字符串 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/

相关文章:

javascript - 如何为 Canvas 应用程序上的共享对话框设置新消息?

javascript - 如何在网格的列中添加 extjs 按钮以显示另一个窗口?

javascript - 在不解决 promise 的情况下使用 $http.post 和 res.redirect

javascript - 我可以使用 Angular2 Universal 仅在客户端中运行组件方法吗?

javascript - 为什么这个函数记录未定义(JavaScript 闭包)

javascript - 即使未经验证,表单也始终会提交

javascript - ReactJS 和继承

javascript - herokuapp 中的端口

javascript - 如何删除字符串结果中的 [ 和 ]

javascript - jQuery - 将 id 元素传递给非匿名回调函数而不使用匿名函数