Hey guys, tried to read some other posts first but none could exactly resolve my case. Also tried it on Gitter twice but no response. I hope some of you might help!
Here’s my JS code so far:
$(function() {
$("#weather").on("click", function() {
$.ajax({
dataType: "json",
url: "https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather",
data: {
lat: navigator.geolocation.getCurrentPosition(function(position) {
return position.coords.latitude;
}) + "",
lon: navigator.geolocation.getCurrentPosition(function(position) {
return position.coords.longitude;
}) + "",
appid: "21b35f3d0bf490f4a1e1f8fa88d5c404"
},
success: function(data) {
console.log(data);
}
});
});
});
I’m trying to provide an additional ‘data’ object to the ajax function so that it appends a query string to the URL with the information the API needs to get me the proper object, i.e. the weather data based on my latitude and longitude. How come it doesn’t work the way I coded it? I expected that the values to the keys ‘lat’ and ‘lon’ were resolved first before used as an argument for creating the query string. But that’s apparently not the case since I always get ‘lon=undefined&lat=undefined’ in my URL. Please help!