Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 内容总是在第二次单击时更新,而不是第一次?_Javascript_C#_Ajax_Promise - Fatal编程技术网

Javascript 内容总是在第二次单击时更新,而不是第一次?

Javascript 内容总是在第二次单击时更新,而不是第一次?,javascript,c#,ajax,promise,Javascript,C#,Ajax,Promise,编写一些JavaScript来更新Bootstrap3Popover中的一些文本。我可能没有正确使用承诺,但我希望文本在我第一次单击时显示在弹出框中,而不是在我单击两次时 Javascript调用用C#编写的本地代理。用于设置popover本身的JavaScript如下所示: $(function () { $('#session-cookie-showcase').popover({ html: true }); $('#session-cookie-s

编写一些JavaScript来更新Bootstrap3Popover中的一些文本。我可能没有正确使用承诺,但我希望文本在我第一次单击时显示在弹出框中,而不是在我单击两次时

Javascript调用用C#编写的本地代理。用于设置popover本身的JavaScript如下所示:

$(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;
        })
    })
})
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))
    });
}
[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");
    }
}
ajax调用如下所示:

$(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;
        })
    })
})
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))
    });
}
[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");
    }
}
最后,C#代理看起来是这样的:

$(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;
        })
    })
})
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))
    });
}
[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");
    }
}
[HttpGet(“hentSessionCookieSomTekst”)]
公共字符串HentSessionCookieSomTekst()
{
字符串userKey=_configuration.GetSection(“SessionKeys:User”).Value;
如果(!string.IsNullOrEmpty(userKey))
{
BrugerObjekt bruger=HttpContext.Session.Get(userKey);
返回JsonConvert.SerializeObject(bruger.ToString('n'));
}
其他的
{
返回JsonConvert.SerializeObject(“NULL”);
}
}

重申一下;电话响了。但它只在第二次单击我的元素时起作用,而在第一次单击时不起作用。我是否可以在认为文本已准备就绪时以某种方式加载文本,而不必再次单击或以其他方式修复此问题?

GetSessionOkieAssString函数本身正在返回一个承诺。你能试试下面的代码吗

getSessionCookieAssString().done(函数(值){
$(“#会话cookie showcase”).data('bs.popover').config.content=value;
});


编辑它。

在@AFriend上检查最上等的答案,结果很好。你想回答这个问题吗?