1
1
123 1232014-11-13 15:48:17
JavaScript
123 123, 2014-11-13 15:48:17

How to put json into a variable?

Good day to all. I need to load json data in infoWindow.

function initialize() {
          var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
          var mapOptions = {
            zoom: 4,
            center: myLatlng
          };
          
          var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
          
          var jsonData = $.ajax({
            url: "http://api.openweathermap.org/data/2.5/find?q=Novosibirsk&units=metric&lang=ru",
            dataType:"json",
            async: false
          });
          console.log(jsonData.message);
          
          var contentString = '<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
            '<div id="bodyContent">'+
            '<p>'+ jsonData.message +'</p>'+
            '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
            'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
            '(last visited June 22, 2009).</p>'+
            '</div>'+
            '</div>';

          var infowindow = new google.maps.InfoWindow({
            content: contentString
          });

          var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Uluru (Ayers Rock)'
          });
          
          google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
          });
        }
      
        google.maps.event.addDomListener(window, 'load', initialize);

Unable to display value. Writes - undefined . Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Ashot, 2014-11-13
@mistergalynsky

First, $.ajax is an asynchronous function, and second, it returns a Promise. This is a very short piece.

$.ajax({
  url: "http://api.openweathermap.org/data/2.5/find?q=Novosibirsk&units=metric&lang=ru",
  dataType:"json",
  async: false
}).done(function(jsonData) {
  var data = $.parseJSON(jsonData);
  // делайте всё что надо с data
} )

W
webinside, 2014-11-13
@webinside

AJAX requests without processing will not work with foreign domains, look for "ajax cross domain"

Y
YemSalat, 2014-11-13
@YemSalat

Understand how ajax works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question