The function failed to obtain JSON data from the generated URL

I want to fetch the latest local weather details using a weather API. Below is the code I am working with:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function generateUrl() {
    var api = "http://api.openweathermap.org/data/2.5/weather?q=";
    var city = "PUNE,IN";
    var units = "&units=metric";
    var appid = "&APPID=**************************"
    var cb = "&callback=JSON_CALLBACK";
    
    return (api + city + units+ appid + cb);
}

$.getJSON(generateUrl(), function(result) {
    $.each(result, function(id, val) {
        //document.write(val);
        //document.getElementById('ModifyMe').innerHTML = '<a id ="'+id+'">'+val+'</a>';
        $("div").append(val + " ");
    });
});
</script>
<div id = 'ModifyMe'><div>

My query: How can I retrieve the JSON data from the URL generated by the generateUrl() function?

Answer №1

   Let jQuery handle the callback function name for you.

Instead of specifying a callback function name, use the "?" placeholder for jQuery to generate it.

var cb = "&callback=?";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing me from integrating angular-cookies into my application?

I'm struggling to resolve this issue where I can't seem to make it work. My aim is to integrate NgCookies (angular-cookies) into my application, but all I'm encountering are errors. This is what I currently have: JS files being included: ...

Should JavaScript be referenced at the start or generated dynamically?

As I continue working on my small web application, I've noticed that the amount of Javascript is increasing. I'm curious about the best practice for loading/referencing Javascript - should it all be loaded at once at the beginning or dynamically ...

Customize the date format of the Datepicker in Angular by implementing a personalized pipe

I am dealing with a datepicker that defaults to the MM/dd/yyyy format, and I need it to adjust based on the user's browser language. For example, if the browser language is English India, then the format should be set to dd/MM/yyyy as shown below. Be ...

There was an issue with parsing the JSON file due to an invalid character, resulting

I'm encountering an issue while attempting to call and parse JSON data. The error message I'm getting is SCRIPT1014: Invalid Character, and this problem seems to be occurring on all browsers, not just Internet Explorer. Jquery: $.ajax({ ...

Adjust the size of the div to match its parent's size when resizing

Can a div be resized to match its parent's size on window resize? Here is the current HTML code: <div class="sliderContainer"> <div id="cyler"> <div class="cy1" style="background-image:url(images/Keggy_Banner_Ie.jpg); back ...

What is the best way to finish up the JavaScript code below?

Having just started learning JavaScript, I am struggling to figure out how to use JavaScript to clear the text in a text box and move it below the box when a user starts typing. I've been watching this tutorial http://codepen.io/ehermanson/pen/KwKWEv ...

Testing for a panic in Golang: A comprehensive guide

func good(json) string { \\do something err = json.Unmarshal(body, &list) if err != nil { panic(fmt.Sprintf("Unable to parse json %s",err)) } } func Testgood_PanicStatement(t *testing.T) { Convey("And Invalid Json return error", ...

Google Maps Info Window with Asynchronous Loading

Need help with loading a Google map asynchronously on your website? Check out the code snippet below: function initialize(lat,lng) { var mapProp = { center: new google.maps.LatLng(lat,lng), zoom:15, mapTypeId: google.maps.MapTypeId.R ...

Allow access to the configuration file for a JavaScript file located in the public directory of an Express application

I have a specific question regarding the folder structure of my Express app. app │ │ └───config │ │ config.js │ │ └───public │ │ │ └───javascripts │ │ exportable.js │ ...

Increase the identifier of my input text when a table row is duplicated through Javascript

I have a table with only one row that contains various elements like textboxes and buttons. I want to clone the table row using the append() function when a button is clicked. However, I am facing an issue - how can I increment the id of the textboxes and ...

Tips for limiting the size of image uploads to under 2 megabytes

I am trying to implement an html select feature that allows users to upload images. <div class="row smallMargin"> <div class="col-sm-6"> Attach Image </div> <div class="col-sm-6"> <input type="file" ng-model="image" accept=" ...

Is there a way to utilize JSON parse for converting deeply nested keys from underscore to camelCase?

A JSON string containing an object is on my mind: const str = `[{"user_id":"561904e8-6e45-5012-a9d8-e2ff8761acf6","email_addr":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa0a3a6a5bc8aaca ...

Something is seriously wrong with the datetime in fullcalendar JavaScript

I've been diving into a tutorial for creating a calendar scheduler in asp.net MVC5 from this link. One issue I'm facing is the datetime being passed and stored as the min value in the database (1/1/0001 12:00:00 AM), almost like it's null b ...

Having trouble parsing an API response in node JS - It appears to be structured as an array

I've recently started working with Node JS and encountered a problem while trying to retrieve status information on public transport using an API. The response I receive appears to be in the form of an array of objects, but I'm struggling to pars ...

How can I apply JavaScript to aggregate child node values and assign them to the parent in JSON data format?

I receive a dynamic JSON from the server, which has varying structures. Each data entry consists of a chapter with stages and/or review sets at the root level. If a stage exists, there will be either a review set array or another stage. The review set cont ...

Managing errors in Node.js when inserting data into MongoDB

Hello everyone, I'm currently working on handling errors in the user signup module using Express. However, I'm facing an issue where the errors are not being handled correctly. Here is my code: handler.post(async (req, res) => { let otp = M ...

How can we efficiently retrieve newly submitted JSON data using AJAX?

Is there an efficient way for a user to input a number, submit it, and have it update a JSON page without refreshing the entire webpage? Can this be achieved using an ajax call? The code below retrieves game data, but I want it to update when the user su ...

Retrieving an assortment of objects from a database and showcasing them using React

Attempting to retrieve an array of objects led me to this issue. Please excuse the messy code, I am still learning. export class App extends Component { state ={ character:[] } componentDidMount(){ fetch('https://swapi.dev/api/people/ ...

Sending a parameter between files in a React application: a step-by-step guide

I am currently working on a Pokedex website where I have Pokemon cards displaying data from a JSON file. When a user clicks on a card, a modal view appears with more detailed information about that specific card. I need help in ensuring that only the deta ...

Creating an HTML button to reveal additional text on the same page

Currently, I am working on a project involving html and javascript. My issue lies in displaying multiple options on the same webpage without switching pages. Essentially, I have a plot and a few buttons on one page, and when a user clicks on any of these b ...