XMLHttpRequest()的未定义变量

时间:2018-04-10 06:21:29

标签: javascript xmlhttprequest

我正在尝试使用javascript从api请求中返回变量。我有非常基本的js技能,但变量ltc总是返回undefined。

<script>
var ltc;
var request = new XMLHttpRequest();

request.open('GET', 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,LTC,NEO,XLM&tsyms=USD', true);
request.onload = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);

    if (request.status >= 200 && request.status < 400) {
        // document.getElementById("test").innerHTML = data.LTC.USD;
        ltc = data.LTC.USD;
    } else {
        console.log('error');
    }
}

request.send();

alert(ltc);

3 个答案:

答案 0 :(得分:0)

您需要考虑+---------------+---------------------------+--------+-------+------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+ | Total Clients | title | month1 | Month | Year | CI_UniqueID | name | CollectionID | Installed / Not Applicable | Required | Unknown Compliant% | NotCompliant% | +---------------+---------------------------+--------+-------+------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+ | 30 | SUG_2017_01_P1_RFC3456 | 01 | Jan | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_E586ED3A-EDD5-4145-98FB-C0B373F7E4CA | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_01-03_P1_RFC2781 | 01 | Jan | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_5AEB5495-8913-4541-B29E-7D55C16E6B68 | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_03_P1_RFC2781 | 03 | Mar | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_ED07143C-B357-454E-B02E-7D81AEE40869 | All Systems SMS00001 | 11 | 15 | 4 | 36.67 | 63.33 | | 30 | SUG_2017_04_P1_RFC3103 | 04 | Apr | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_2722FDDB-6D6B-407F-A0CE-063372571E82 | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_04-05_P1_RFC2781 | 04 | Apr | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_10D742F8-FB4B-4E19-BF05-5210C790F440 | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_06_P1_RFC3123 | 06 | June | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_3063A272-0DF9-4033-94E2-C52AF1CFD4BC | All Systems SMS00001 | 25 | 1 | 4 | 83.33 | 16.67 | | 30 | SUG_2017_10_P1_RFC3103 | 10 | Oct | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_9C3F338B-E8BA-4AB5-8ECF-1EA8729825DA | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_11_P1_RFC3103 | 11 | Nov | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_B42D69FC-2564-4542-8D5B-F5348A4080FF | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2017_12_P1_RFC3103 | 12 | Dec | 2017 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_5C3AD217-7747-4BAC-AD06-3851014BCB94 | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2018_01_P1_RFC3103 | 01 | Jan | 2018 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_A49E2378-BCB6-40BE-BE84-735CCFBEE43F | All Systems SMS00001 | 25 | 1 | 4 | 83.33 | 16.67 | | 30 | SUG_2018_02_P1_RFC3118 | 02 | Feb | 2018 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_E6CBD108-2B1C-4C94-85F8-57174BEC34C4 | All Systems SMS00001 | 25 | 1 | 4 | 83.33 | 16.67 | | 30 | SUG_2018_03_P1_RFC3128 | 03 | Mar | 2018 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_6AA69C57-9532-4ED1-BA40-1540C840BD69 | All Systems SMS00001 | 26 | 0 | 4 | 86.67 | 13.33 | | 30 | SUG_2018_05_P1_RFC3104 | 05 | May | 2018 | ScopeId_A66804AF-F55C-40D6-8AAF-82CF49CC1E5B/AuthList_D3A10469-5DE5-4998-9C59-877D3BC7225F | All Systems SMS00001 | 12 | 14 | 4 | 40 | 60 | +---------------+---------------------------+--------+-------+------+---------------------------------------------------------------------------------------------+----------------------+---------------+-----------------------------+----------+---------------------+---------------+ 异步执行 。这意味着JavaScript会触发该请求,然后立即继续执行而不等待请求完成,即不等到数据到达并且不等到请求处理程序已经执行。

因此,您的行

XMLHttpRequest

在请求完成之前和请求处理程序执行之前执行。由于请求处理程序是alert(ltc) 按预期设置的唯一位置,因此在执行上面的行时,ltc仍未定义。

如果您将ltc移动到请求处理程序中,它将按预期工作。

答案 1 :(得分:0)

这是因为您的警报会立即运行,并且在警报运行时您还没有得到响应。如果将警报放在onload函数内,则可以获得正确的值。

var ltc;
var request = new XMLHttpRequest();

request.open('GET', 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,LTC,NEO,XLM&tsyms=USD', true);
request.onload = function (e) {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);
    
    if (request.status >= 200 && request.status < 400) {
        // document.getElementById("test").innerHTML = data.LTC.USD;
        ltc = data.LTC.USD;            
    } else {
        console.log('error');
    }
    alert(ltc);
}

request.send();

如果你想在外面,你可以把它放在一个函数中,并在onload事件中调用该函数。

var ltc;
var request = new XMLHttpRequest();

request.open('GET', 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,LTC,NEO,XLM&tsyms=USD', true);
request.onload = function (e) {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);
    
    if (request.status >= 200 && request.status < 400) {
        // document.getElementById("test").innerHTML = data.LTC.USD;
        ltc = data.LTC.USD;
        displayAlert();
    } else {
        console.log('error');
    }    
}

request.send();

function displayAlert() {
  alert(ltc);
}

答案 2 :(得分:0)

因为您执行异步请求在http请求完成之前执行警报。您需要像这样从请求处理程序执行它。

<script>
var ltc;
var request = new XMLHttpRequest();

request.open('GET', 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,LTC,NEO,XLM&tsyms=USD', true);
request.onload = function () {
    // Begin accessing JSON data here
    var data = JSON.parse(this.response);

    if (request.status >= 200 && request.status < 400) {
        // document.getElementById("test").innerHTML = data.LTC.USD;
        ltc = data.LTC.USD;
        alertLtc();
    } else {
        console.log('error');
    }
}

request.send();

function alertLtc() {
    //Note: console.log(ltc); is a better way to test variables.
    alert(ltc);
}