Is my JavaScript coding accurate?

I'm working on extracting JSON data from my Rest API to display in my application. I'm wondering if the function renderUserState is being called, as the rest of my code seems to be functioning correctly.

function testSubmit() {
    var card = getQueryVariable("select_card");
    var action = getQueryVariable("select_action");

    var urlGet = "/test/api/getting-ready?cardId=" + card + "&action=" + action;

    $.ajax({
        type : 'GET',
        url : urlGet,
        dataType : 'json',
        encode : true
    }).done(renderUserState);
}

function renderUserState(userState){
    $("#gold").text(userState.goldAmount);
}

Any assistance would be greatly appreciated!

Answer №1

Yes, your code is accurate. Just make sure to invoke a function in the 'done' method as it will receive the data from your endpoint. Give it a try by clicking on the button and checking the log for results.

function fetchData() {
  var url = "https://jsonplaceholder.typicode.com/posts";

    $.ajax({
        type : 'GET',
        url : url,
        dataType : 'json',
        encode : true
    }).done((userData) => {
      displayUserData(userData);
      });
}

function displayUserData(userData){
    console.log(userData);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onClick="fetchData();">Click Here</button>

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

Keep your data safe and protected within a Node CLI application

Currently developing a NodeJS command-line application that interacts with an API to provide data to users. To access the public API, users need an API token. The CLI will be globally installed on users' machines using npm i -g super-cool-api-cli. Up ...

Implementing pagination in Firestore using React-Redux

I'm currently working on implementing pagination with Firebase and React Redux Toolkit. I've grasped the logic behind it, but I'm facing challenges when integrating it with Redux. Initially, my approach was to store the last document in the ...

In JavaScript, the price can be calculated and displayed instantly when a number is entered into a form using the input type 'number'

Is there a way for me to automatically calculate the price and display it as soon as I enter a number into my form? Currently, the price is only displayed after I press submit. <script type="text/javascript"> function calculatePrice() { ...

Ways to solely cache spa.html using networkfirst or ways to set up offline mode with server-side rendering (SSR)

I am facing an issue with my application that has server-side rendering. It seems like the page always displays correctly when there is an internet connection. However, I am unsure how to make Workbox serve spa.html only when there is no network available. ...

Highly complex regular expressions in nth-check causing inefficiencies during npm installation of react-router-dom

Feeling new and inexperienced with how to react, I attempted to execute the command "npm i react-router-dom" but it ended up stopping the download process and displaying several errors. In my search for a solution, I stumbled upon this reference to some ...

Positioning a div at the bottom of a webpage without using absolute positioning in HTML

Check out the fiddle. I've included some CSS to position the tab on the right side. My goal was to have those tabs in the lower right corner of the container. If I use absolute positioning for the div, it messes up the tab content width. I tried ad ...

Utilizing D3.js: Applying Multiple Classes with Functions

My current project involves using D3.js and I've encountered a particular challenge that has me stumped. In the CSV file I'm working with, there are columns labeled "Set" and "Year". My goal is to extract values from these columns and use them a ...

Is there a way for me to retrieve the "title" from this API?

I recently came across this code snippet on stackoverflow that retrieves data for a single YouTube video using its video id. However, I'm encountering an issue with extracting the "title" from the URL. While it functions correctly in my web browser, t ...

Web application using HTML5, AJAX, and a manifest file for offline usage

Looking for help with an issue on my website: Currently trying to put it offline. Using the manifest file: Encountering an issue with the left and right arrow navigation while offline. The browser shows an xmlhttprequest error, similar to the one found ...

React Higher Order Component (HOC) encountered an ESLint issue: spreading props is not

Does eslint lack intelligence? The Higher Order Component (HOC) is quite generic, so I struggle to specify the incoming options/props as they are dynamic based on the component being wrapped by this HOC at any given time. I am encountering an error statin ...

Display a division in C# MVC 4 when a boolean value is true by using @Html.DropDownList

I have multiple divs stacked on top of each other, and I want another div to appear when a certain value is selected. I'm familiar with using JavaScript for this task, but how can I achieve it using Razor? Below is a snippet of my code: <div id=" ...

I am currently in the process of verifying email addresses

I attempted to validate multiple email addresses from a txt file and then save the valid emails to another txt file using nodejs. Unfortunately, it did not work as expected. Despite successfully reading the file, all emails were deemed invalid, even though ...

jsonAn error occurred while attempting to access the Spotify API, which resulted

Currently, I am working on acquiring an access Token through the Client Credentials Flow in the Spotify API. Below is the code snippet that I have been using: let oAuthOptions = { url: 'https://accounts.spotify.com/api/token', method: ' ...

Challenge with collapsing a button within a Bootstrap panel

Check out this snippet of code: <div class="panel panel-default"> <div class="panel-heading clearfix" role="tab" id="heading-details" data-toggle="collapse" data-target="#details" data-parent="#panel-group" href="#details"> <h4 c ...

Trouble with defining variables in EJS

Recently delving into the world of node development, I encountered an issue with my EJS template not rendering basic data. I have two controllers - one for general pages like home/about/contact and another specifically for posts. When navigating to /posts ...

Using Props with jQuery in React Components: A Comprehensive Guide

I trust you comprehend this straightforward example. I attempted to modify the background color of my HTML element during initial rendering by managing it in a React Component with a touch of jQuery assistance. Here is the code within my React Component ...

Determine the previous day's date using JavaScript by subtracting one day

I am facing an issue while attempting to store a specific date format in MongoDB. Instead of saving the date as intended, it is consistently registering a day earlier than specified. For instance - db.test.insert({name: "test1", dob: new Date(1986, 11, ...

Why does starting up the Firebase emulators trigger the execution of one of my functions as well?

Upon running firebase emulators:start --only functions,firestore, the output I receive is as follows: $ firebase emulators:start --only functions,firestore i emulators: Starting emulators: functions, firestore ⚠ functions: The following emulators are ...

Leveraging Jquery and an API - restricted

I have the opportunity to utilize a search API that operates on JSON format through a URL GET. This particular API has a reputation for imposing quick bans, with an appeal process that can be lengthy. If I were to integrate this API into my website using ...

Guide to testing express Router routes with unit tests

I recently started learning Node and Express and I'm in the process of writing unit tests for my routes/controllers. To keep things organized, I've split my routes and controllers into separate files. How should I approach testing my routes? con ...