Why isn't AJAX working?

  • 0
    There is a script, for some reason it does not work in the browser, it does not even display an error in the console, the path to the json file seems to be correct.
    const inputRub = document.querySelector('#rub'),
          inputUsd = document.querySelector('#usd');
    
    inputRub.addEventListener('input', () => {
        const request = new XMLHttpRequest();
    
        request.open('GET', 'current.json');
        request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
        request.send();
    
        request.addEventListener('load', () => {
            if(request.status === 200){
                const data = JSON.parse(request.response);
                console.log(request.response);
                inputUsd.value = (+inputRub.value / data.current.usd).toFixed(2);
            }else {
                inputUsd.value = 'error';
            }
        });
    });


    <div class="converter">
                <div class="monay">
                    <h2 class="descr">RUB</h2>
                    <input type="number" id="rub">
                </div>
                <div class="monay">
                    <h2 class="descr">USD</h2>
                    <input type="number" id="usd">
                </div>
            </div>


    JavaScript Anonymous, Jun 30, 2019

  • 2 Answers
  • 0
    Put send () after addEventListener ().

    The response is likely to come before the event handler can be assigned.
    Violet Rivera

  • 0
    Everything works great



    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!