0

Writing some JavaScript to update some text in a Bootstrap 3 Popover. I'm probably not using Promises right, but I want the text to appear in the popover the first time I click, not when I've clicked it twice.

The Javascript calls a local Proxy written in C#. The JavaScript for the setup of the popover itself looks like this:

$(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;
        })
    })
})

The ajax call looks like this:

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 finally the C# proxy looks like this:

[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");
    }
}

So to reiterate; The call works. But it only works on the second click of my element, never on the first. Can I somehow make the text load in whenever it is deemed ready instead of having to click again or fix this some other way?

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • Check the most upvoted answer on [this question](https://stackoverflow.com/questions/14754619/jquery-ajax-success-callback-function-definition) – A Friend Oct 31 '19 at 10:48
  • @AFriend That worked out well. Would you like to make it an answer to this? – OmniOwl Oct 31 '19 at 12:05

1 Answers1

1

getSessionCookieAsString function is returning a promise itself. can you try below code.

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

edited it.

Amit Gupta
  • 21
  • 3