我得到了#34;未定义"在发出HTTP get请求时,但只是第一次

时间:2018-02-27 04:48:27

标签: javascript jquery http get

我得到"未定义"在发出HTTP get请求时,但仅在我第一次发出请求时。第二次和代码似乎完全正常。

以下是我的代码:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        let myData = "";
        let dataTest = "";
        $(document).ready(function(){
            $("button").click(function(){
                $.get("https://api.coinmarketcap.com/v1/ticker/ethereum/", function(data, status){
                    myData = data[0];
                });
                document.getElementById("p1").innerHTML = (myData.price_usd);
            });
    });
    </script>
</head>
<body>
    <p>If you want to get the price of Ethereum, press the following button:</p>
    <button>Submit</button>
    <p id="p1">
    </p>
</body>

2 个答案:

答案 0 :(得分:2)

将分配放入let day = _.findWhere(this.availabilityDays, {D: moment($scope.model.BookDate).format('YYYY-MM-DD')}); this.$scope.BookingPeriods.splice(0); for (let period of (<{P: string}>day).P) { this.$scope.BookingPeriods.push(period); } 异步请求的回调中的innerHTML。你不知道你的电话什么时候结束,所以为什么你需要在回调中写它。在第一次,你分配然后呼叫完成

get

答案 1 :(得分:0)

import { Keychain } from '@ionic-native/keychain';

constructor(private keychain: Keychain) { }

...

this.keychain.set(key, value).then(() => {
  this.keychain.get(key)
    .then(value => console.log('Got value', value))
    .catch(err => console.error('Error getting', err));
})
.catch(err => console.error('Error setting', err));

由于其<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> let myData; let dataTest; $(document).ready(function(){ $("button").click(function(){ $.get("https://api.coinmarketcap.com/v1/ticker/ethereum/", function(data, status){ myData = data[0]; logg(); }); }); }); function logg(){ document.getElementById("p1").innerHTML = (myData.price_usd); } </script> </head> <body> <p>If you want to get the price of Ethereum, press the following button:</p> <button>Submit</button> <p id="p1"> </p> </body> 以及完成时将async分配给data[0]然后,您将更改文档的innerHtml。