简体   繁体   English

从 URL 获取 json 响应

[英]GET json response from URL

I understand that this question asked MANY times, but I can't figure out how to make it work in my circumstances我知道这个问题问了很多次,但我不知道如何让它在我的情况下工作

It is a simple thing.这是一件简单的事情。 I have url = WWW, but opening it in web browser you will see JSON.我有 url = WWW,但是在 Web 浏览器中打开它,您将看到 JSON。

I need to use JavaScript to get this JSON from URL and use it further.我需要使用 JavaScript 从 URL 获取这个 JSON 并进一步使用它。

<script>
var data;

$.getJSON("http://XXX?callback=?").done(function( data ) {
    console.log('hello', data);
    data = data;
    initMap();
});

function initMap() {    

  //response from URL have to be used here
  data.forEach((item) => {

    });
}

    </script>

DOes anyone know how to solve it?有谁知道如何解决它? Ideally by using ASYNC理想情况下使用 ASYNC

This is FULL CODE:这是完整代码:

    <script>
// data from server
$.getJSON("http://XXX?callback=?").then(function( data ) {
    console.log('hello', data);
    initMap(data);
});

// place you want to initially center the map on
const center = {
  lat: 51.509865,
  lng: -0.118092
}

function initMap(data) {
  // set up map
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: center.lat, lng: center.lng}
  });

  // loop over data from server
  data.forEach((item) => {
    // build a infowindow add dump the product table into it.
    var infowindow = new google.maps.InfoWindow({
      content: item.Products
    });

    // add and position the marker on the map
    var marker = new google.maps.Marker({
      position: {lat: item.Latitude, lng: item.Longitude},
      map: map,
      title: item.StoreName
    });

    // and event for opening the infowindow
    marker.addListener('click', function() {
      infowindow.open(map, marker);
    });
  });
}

google.maps.event.addDomListener(window, 'load', initMap);

</script>

JSON Looks like this : JSON 看起来像这样:

[
    {
        "LatLan": "-3,22",
        "Latitude": 22,
        "Longitude": -3,
        "StoreName": "XXX",
        "Products": "XXX"
    },
    // carry on...
]
data = data

This will not work as you expect.这不会像您期望的那样工作。

I would recommend passing the data into your initMap method: initMap(data)我建议将数据传递到您的initMap方法中: initMap(data)

function initMap(data) {    

  //response from URL have to be used here
  data.forEach((item) => {

    });
}

You would be better using a construct like this so you're not using a global variable, that's frowned upon.您最好使用这样的构造,这样您就不会使用全局变量,这是不受欢迎的。 using a top level global variable like data you could stomp on any other modules that are using a global data variable (they shouldn't be, but...).使用像 data 这样的顶级全局变量,您可以踩踏使用全局数据变量的任何其他模块(它们不应该,但是......)。 Also, the line data = data is not going to assign to the global data variable, since the data variable that's in scope is the one inside the done block (I changed it to then()).此外,行data = data不会分配给全局数据变量,因为范围内的数据变量是 done 块内的变量(我将其更改为 then())。

$.getJSON("http://XXX?callback=?").then(function( data ) {
    console.log('hello', data);
    initMap(data);
});

function initMap(data) {    

  //response from URL have to be used here
  data.forEach((item) => {

    });
}

Here is one way to loop through JSON:这是循环 JSON 的一种方法:

 var tableRow = ''; $.ajax({ url: 'https://randomuser.me/api/?&results=50&inc=name,email', method: 'GET', success: function(data) { var items = ''; for (var i = 0; i < data.results.length; i++) { items += '<tr><td>' + data.results[i].name.first + ' ' + data.results[i].name.last + '</td><td>' + data.results[i].email + '</td><td>' + data.results[i].email + '</td></tr>'; } $("#dataTable tbody").html(items); console.log(data); }, error: function() { var tableRow = '<tr><td>There is no data to display</td></tr>'; console.log(tableRow); $("#dataTable tbody").html(tableRow); } });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <table class="table" id="dataTable"> <thead> <tr> <th>Full Name</th> <th>Email</th> <th>Address</th> </tr> </thead> <tbody></tbody> </table> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM