Andy Andy - 5 months ago 13
Javascript Question

Everything works but the Ajax

Making a call to the openweather API. When I input the URL into a browser, I can see the JSON object. However my ajax below is not allowing anything to appear in my HTML. Even if I want to document.write the entire json object, I cannot. I can output the lat and long just find so the geolocation is working. Not sure what's wrong with this code.

Here's the HTML:

<div class="container">
<div class="col-lg-12 header">
<h1>My Location</h1>
<div class="row text-center box">
<div class="col-lg-12" id="data">Lat Long</div>

Here's the JS:

$(document).ready(function() {
if (navigator.geolocation) {
function main(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
var api = "" + lat + "&lon=" + long + "&appid=edb786f28101411599bcf4bfc8b6edbc";
dataType: 'json',
url: api,
success: function(data) {
city =;


Try $.getJson instead of $.ajax: $.getJSON( { dataType: 'jsonp', url: api, success: function(data) { city =; $("#data").html(city); }

In the HTML, make sure to add jquery:

<script src=""></script> <script src="a.js"></script> // whatever your js file is named

Without modifying your code, the page asked to access my location and then displayed my current city. I'm not sure what it is supposed to output though. What are you expecting the output to be?