在同一页面上调用API两次,在WordPress中第二次失败

ldioqlga  于 2023-01-16  发布在  WordPress
关注(0)|答案(1)|浏览(145)

代码在codepen中运行良好,但当尝试在WordPress页面中使用短代码运行时,它没有运行。可能是什么原因。此代码下面的所有剩余内容也没有显示。特别是当尝试在页面中运行js时,没有显示任何内容。

<script>
    // Fetch the exchange rates from the API
    fetch(`https://api.fastforex.io/fetch-multi?from=AED&to=USD,EUR,GBP,INR,AUD,CAD,SGD,CHF,MYR,JPY&api_key=xxxxx-xxx-xxx`)// API key on purpose
      .then(response => response.json())
      .then(data => {
        // Update the exchange rates in the table
        document.getElementById("eur").innerHTML = data.results.EUR;
document.getElementById("gbp").innerHTML = data.results.GBP;
document.getElementById("chf").innerHTML = data.results.CHF;
document.getElementById("eur").innerHTML = data.results.EUR;
document.getElementById("gbp").innerHTML = data.results.GBP;
document.getElementById("chf").innerHTML = data.results.CHF;
document.getElementById("usd").innerHTML = data.results.USD;
document.getElementById("inr").innerHTML = data.results.INR;
document.getElementById("aud").innerHTML = data.results.AUD;
document.getElementById("cad").innerHTML = data.results.CAD;
document.getElementById("sgd").innerHTML = data.results.SGD;
document.getElementById("myr").innerHTML = data.results.MYR;
document.getElementById("jpy").innerHTML = data.results.JPY;

      });
  </script>
</body>
zbsbpyhn

zbsbpyhn1#

你在使用它之前排队了吗?

function enqueue_exchange_rate_script() {
    wp_enqueue_script( 'exchange-rate-script', get_stylesheet_directory_uri() . '/exchange-rate-script.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_exchange_rate_script' );


fetch(`https://api.fastforex.io/fetch-multi?from=AED&to=USD,EUR,GBP,INR,AUD,CAD,SGD,CHF,MYR,JPY&api_key=your_api_key`)
    .then(response => response.json())
    .then(data => {
        // Update the exchange rates in the table
        document.getElementById("eur").innerHTML = data.results.EUR;
        document.getElementById("gbp").innerHTML = data.results.GBP;
        document.getElementById("chf").innerHTML = data.results.CHF;
        document.getElementById("usd").innerHTML = data.results.USD;
        document.getElementById("inr").innerHTML = data.results.INR;
        document.getElementById("aud").innerHTML = data.results.AUD;
        document.getElementById("cad").innerHTML = data.results.CAD;
        document.getElementById("sgd").innerHTML = data.results.SGD;
        document.getElementById("myr").innerHTML = data.results.MYR;
        document.getElementById("jpy").innerHTML = data.results.JPY;
    });

相关问题