JavaScript json调用API

JavaScript json调用API,javascript,json,api,Javascript,Json,Api,我想在调用代码中提到的API后显示纬度和经度。然后我想将纬度和经度值作为参数传递,以进行另一个API调用来显示格式化的地址。但是这个脚本给了我一个错误 function loadJSON(callback) { var xobj = new XMLHttpRequest(); xobj.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Pune", true); xobj.onreadyst

我想在调用代码中提到的API后显示纬度和经度。然后我想将纬度和经度值作为参数传递,以进行另一个API调用来显示格式化的地址。但是这个脚本给了我一个错误

function loadJSON(callback) {

  var xobj = new XMLHttpRequest();
  xobj.open("GET", "https://maps.googleapis.com/maps/api/geocode/json?address=Pune", true);
  xobj.onreadystatechange = function() {
    if (xobj.readyState == 4 && xobj.status == "200") {
      callback(xobj.responseText);
    };
  }
  xobj.send();
}

function init() {
  loadJSON(function(responseText) {
      // Parse JSON string into object
      var myObj = JSON.parse(responseText);

      console.log(myObj.results[0].geometry.bounds.northeast.lat);
      var lat=myObj.results[0].geometry.bounds.northeast.lat;
      var lon=myObj.results[0].geometry.bounds.northeast.lng;
      console.log(myObj.results[0].geometry.bounds.northeast.lng);

      address(lat, lon);

    });
}

init();

function address(lat, log){
    var qstr=lat+","+log
    var link="http://maps.googleapis.com/maps/api/geocode/json?latlng="+qstr+"&sensor=true";
    console.log(link);


    fetchAddress(function(responseText) {
      // Parse JSON string into object
      var myObj = JSON.parse(responseText);


      var adress=myObj.results[0].formatted_address;
      console.log(adress);




    }, link);

}

function fetchAddress(callback, link) {

  var xobj = new XMLHttpRequest();
  xobj.open("GET", link, true);
  xobj.onreadystatechange = function() {
    if (xobj.readyState == 4 && xobj.status == "200") {
      callback(xobj.responseText);
    };
  }
  xobj.send();
}

最有可能的错误是在您的链接

使用
var link=“//maps.googleapis.com/maps/api/geocode/json?latlng=“+qstr+”&sensor=true”


您没有指定http或https,而是得到了什么错误?如果你说出你所面临的确切问题,人们可以帮助你更快地缩小范围,找到解决方案。这不是一个错误,请参阅