在 JavaScript 中重新加载本地文件

问题描述 投票:0回答:1

我是一个 JavaScript 新手。我主要用 C# 编写代码,所以我对此很陌生。

我正在尝试将 PowerBI 嵌入式报表嵌入到 WPF 应用程序中。我可以做到这一点,我可以正确显示报告并在计时器上刷新数据。

但是,随页面加载的访问令牌在一段时间后就会过期。 我将令牌更新到本地文件中,并在 HTML 页面加载时读取该本地文件。

我不想在 WPF Web 控件中重新加载 HTML 页面来刷新访问令牌,我想重新加载文件的内容并更新实时使用的 PowerBI 访问令牌。

当我尝试在计时器上重新加载新令牌时,保存令牌的变量不会更新。它仍在使用新令牌。

如何重新加载“data.json”文件的内容以更新 PowerBI“report.setAccessToken”?

参见“我的方法”。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Power BI </title>
    <!-- Include Power BI JavaScript library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
  <script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"></script>
</head>
<body>

    <div id="embedContainer" style="height: 1080px;"></div>
    <script src="app.js" async></script>

    <!-- File which contains the access token -->
    <script type="text/javascript" src="data.json" id="data"></script> 

</body>
</html>

app.js

let loadedResolve, reportLoaded = new Promise((res, rej) => { loadedResolve = res; });
let renderedResolve, reportRendered = new Promise((res, rej) => { renderedResolve = res; });

models = window['powerbi-client'].models;

function embedPowerBIReport() {
    
    var mydata = JSON.parse(data)
    let accessToken = mydata[0].accessToken;

    // Read embed URL
    let embedUrl = "https://app.powerbi.com/reportEmbed?reportId=xxxxx";

    // Read report Id
    let embedReportId = "xxxx";

    // Read embed type from radio
    let tokenType = "Bearer";

    // We give All permissions to demonstrate switching between View and Edit mode and saving report.
    let permissions = models.Permissions.All;

    // Create the embed configuration object for the report
    let config = {
        type: 'report',
        tokenType: tokenType == '0' ? models.TokenType.Aad : models.TokenType.Embed,
        accessToken: accessToken,
        embedUrl: embedUrl,
        id: embedReportId,
        permissions: permissions,
        settings: {
            panes: {
                filters: {
                    visible: false
                },
                pageNavigation: {
                    visible: false
                }
            },
            bars: {
                statusBar: {
                    visible: false
                }
            }
        }
    };

    // Get a reference to the embedded report HTML element
    let embedContainer = $('#embedContainer')[0];

    // Embed the report and display it within the div container.
    report = powerbi.embed(embedContainer, config);

    // report.off removes all event handlers for a specific event
    report.off("loaded");

    // report.on will add an event handler
    report.on("loaded", function () {
        loadedResolve();
        report.off("loaded");
    });

    // report.off removes all event handlers for a specific event
    report.off("error");

    report.on("error", function (event) {
        console.log(event.detail);
    });

    // report.off removes all event handlers for a specific event
    report.off("rendered");

    // report.on will add an event handler
    report.on("rendered", function () {
        renderedResolve();
        report.off("rendered");
    });

    window.setInterval(function () {
        report.refresh();
    }, 30 * 1000);

}

setInterval(myMethod, 20000);

function myMethod( )
{
     var mydata = JSON.parse(data)
     accessToken = mydata[0].accessToken;
     report.setAccessToken(accessToken);
     
     
     alert(mydata[0].accessToken);

}

async function loadReport() {

    embedPowerBIReport();
    
    await reportLoaded;

    await reportRendered;
    
}

loadReport();

数据.json

data = '[{"accessToken" : "xxxxxxx"}]';
javascript wpf powerbi powerbi-embedded
1个回答
0
投票

如何刷新访问令牌?

假设您使用 HTTP 请求来获取和存储令牌,我建议您在 JS 代码中发出该请求,而不是单独发出请求并将结果存储在文件中。如果您在需要使用时正确获取访问代码,那么它就不会过期。如果您需要有关 JS 中浏览器 HTTP 请求的指导,我会遵循此帖子接受的答案:

https://stackoverflow.com/questions/247483/http-get-request-in-javascript

© www.soinside.com 2019 - 2024. All rights reserved.