Does the content only refreshes after the second click, never the first one?

Trying to figure out how to update text in a Bootstrap 3 Popover using JavaScript. I suspect my use of Promises is incorrect because the text only appears in the popover after clicking it twice.

The JavaScript code involves calling a local C# Proxy. Here's an excerpt from the setup of the popover:

$(function () {
    $('#session-cookie-showcase').popover({
        html: true
    });
    $('#session-cookie-showcase').on('show.bs.popover', function () {
        Promise.resolve(getSessionCookieAsString()).then(function (value) {
            $('#session-cookie-showcase').data('bs.popover').config.content = value;
        })
    })
})

This is how the ajax call is structured:

async function getSessionCookieAsString(callback) {
    return $.ajax({
        type: "GET",
        url: AppName + '/api/omitted/hentSessionCookieSomTekst',
        dataType: "json",
        contentType: "application/json",
        success: ((result) => function (result) { return result }),
        error: ((XMLHttpRequest, textStatus, errorThrown) =>
            handleWebserviceCallError(XMLHttpRequest, textStatus, errorThrown, false))
    });
}

And here's a snippet of the C# proxy code:

[HttpGet("hentSessionCookieSomTekst")]
public string HentSessionCookieSomTekst()
{
    string userKey = _configuration.GetSection("SessionKeys:User").Value;
    if (!string.IsNullOrEmpty(userKey))
    {
        BrugerObjekt bruger = HttpContext.Session.Get<BrugerObjekt>(userKey);
        return JsonConvert.SerializeObject(bruger.ToString('n'));
    }
    else
    {
        return JsonConvert.SerializeObject("NULL");
    }
}

In summary, the call works but only on the second click. Is there a way to make the text load in on the first click or resolve this issue differently?

Answer №1

Is the getSessionCookieAsString function returning a promise in itself? You can test it with the code below.

getSessionCookieAsString().done(function (value) {
            $('#session-cookie-showcase').data('bs.popover').config.content = value;
        });

I made some edits to it.

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

Convert the object containing arrays to a boolean value, which will be true if at least one of the arrays is not empty

Here's the scenario: searchCriteria: { filter1: [?], filter2: [?], filter3: [?], and so on } I am aiming for a boolean output that indicates whether any of the filter arrays contain data (i.e. are not empty). Something along the lines of: co ...

Is there a way to prevent my jQuery from triggering the <a href> unless the ajax post is successful?

I am attempting to update the database and redirect the user's browser to a new page with just one click. Here is how the HTML appears: <a id='updateLiveProgress' style='width:118px;' href='~link~'>Click here</ ...

Is there a way to modify the dimensions of this container? (CSS)

After performing a search and clicking on a result, a white bubble pops up on the map. I am looking to use CSS to make this bubble smaller as it is currently too large. Can anyone provide guidance on how to achieve this? ...

Avoid loading Google Maps JavaScript library multiple times to improve website performance

I'm facing an issue with loading Google Maps API script twice in a Bootstrap modal through AJAX. Initially, I have a button that, when clicked, triggers an AJAX call to load an HTML file into the modal. The HTML file contains the Google Maps API scrip ...

Using JQuery to reverse the action of hiding an image

Having some trouble with my Javascript and JQuery skills, so bear with me. I've set up a gallery of images where users can drag and drop them into a designated drop zone. Everything works fine when dragging an image from the gallery to the drop zone a ...

Steps for displaying a loading gif animation prior to retrieving text content using JavaScript

Before fetching the PHP result, I want a loading GIF image to display within the div section. <!DOCTYPE html> <html> <head> <title> CTR Calculator Tool | Free Click Through Rate Online Calculator </title> & ...

A guide on effectively mocking a Vuex store within the parentComponent of VueJS test-utils

I am currently using Jest in conjunction with vue-test-utils to test the reaction of a child component to an $emit event triggered by the parent component. The VueJS test-utils library offers a parentComponent option that can be utilized when mounting or ...

Guide to implementing a datepicker on a webpage using Laravel

I am having trouble displaying the datepicker using the code below in my Laravel 4.2 application on Firefox. Is there a better way to display datepickers? Are there any CDNs available for this purpose? <table class="table table-striped"> <t ...

Link Array with Google Charts

I've been struggling for a long time to connect this array to a Google chart with no success. I would really appreciate some assistance in identifying what mistake I've made. I have created a jsfiddle where the array appears to be correct, and wh ...

Deserializing an integer using Newtonsoft.Json could potentially yield a list of items

I am encountering an issue where the JSON response may include either a single INT or a list of INTs under the "ErrorCode" key. How can I properly deserialize this using Newtonsoft.Json's JsonConvert? When I define the object as a List, it fails to d ...

When making a JavaScript ajax request, Django is not being activated

I'm struggling to implement a feature where the text on a webpage changes based on the selection made in a dropdown menu. My JavaScript code doesn't seem to be calling the Django function correctly, so the placeholder text remains unchanged. Jav ...

Modifying deeply nested properties in Vue.js

Within my Vue.js application, I've created a method to update the src attribute of images whenever a file is chosen from a file input field. This method is triggered like so: <input type="file" accept=".jpg,.png" @change="updateSrc($event, 'p ...

Guide on clicking an element within a hidden iframe using Python Selenium

I am facing a challenge in finding elements within an iframe tag. Surprisingly, the HTML source does not contain any iframe tags. However, upon inspecting the element, I can see that there is indeed an iframe tag present. How can I tackle this issue using ...

Dynamic Cascading Selection

I am new to using JQuery Ajax and I need help with creating a PHP script to read the subcategory list based on the selected main category. I have implemented a jQuery AJAX function in my asset_add.php file. <script src="jquery.min.js"></script> ...

Dynamic reloading of a div with form data using jQuery's AJAX functionality

I am currently developing an online visitor chat software using PHP and MySQL. My goal is to load the page when the submit button is clicked. Submit Button ID: send Visitor ID: vid Chat ID: cid Below is the snippet of code for an Ajax request that I hav ...

determining the file size of images being loaded remotely by retrieving their image URLs

There is a straightforward regex function in jQuery that I'm using to automatically add an image tag to image URLs shared by users. This means that when a user posts something like www.example.com/image.jpg, the image tag will be included so that user ...

How can I store Ajax data in integer format?

I am attempting to retrieve data from a PHP page using ajax and then save it in a variable that will be used in a while loop. I have successfully displayed the number on the screen, confirming that the data retrieval is functioning. However, I am encounte ...

How to toggle a class using ng-click in AngularJS

Currently, I am working on a project using AngularJS for my website. One specific feature involves having a form that is initially hidden with "display:none" set deliberately. There's also a button labeled create. My goal is to have the form toggle b ...

What is the reason behind only being able to toggle half of the element's classes?

My current project involves using jQuery's .toggleClass() function to implement a favorite button within multiple <div> elements, each containing an <a> element and an <i> element. I am toggling between two Font Awesome icons by swit ...

Leveraging AJAX functionality within Orchard CMS

Attempting to pass Json data to Controller using Ajax (with reference to a similar discussion on AntiForgery and JSON compatibility), the following code was implemented: Ajax: var self = this; self.Url = ko.observable(); self.Description = ko.observable( ...