Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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
} )
AJAX requests without processing will not work with foreign domains, look for "ajax cross domain"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question