1
0

Access to XMLHttpRequest at 'file:///C:/Users/User/Desktop/it/js/JSON/currency.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

json.html:44 The specified value "wrong" cannot be parsed, or is out of range.

GET file:///C:/Users/User/Desktop/it/js/JSON/currency.json net::ERR_FAILED

это мой код и выдает такие ошибки;

let inputRub=document.getElementById("rub"),
        inputUsd=document.getElementById("usd");

    inputRub.addEventListener('input',()=>{
        let request=new XMLHttpRequest();

        request.open('GET','currency.json');
        request.setRequestHeader('Content-type','application/json;charset=utf-8');
        request.send();

        request.addEventListener('readystatechange',function () {
            if(request.readyState===4 && request.status==200){
                let data=JSON.parse(request.response);
                inputUsd.value=inputRub.value/data.usd;
            }
            else{
                inputUsd.value="wrong";
            }
        });
    });

Davit Poghosyan
3 years ago






У меня была проблема что не работало тоже. Мой ответ уже поздний, может другим поможет.

Проблему я решил по средством дабавления в HTML к тегам input  id ( в уроке было id="rub" и в другом input id="usd").

После этого заработало.

У вас нету пробелов что может привести к ошибке.

отсутсвует указание папки на строке -  request.open('GET','currency.json'); Конечно если вы использует папку js и там храните файлы как вы по идеи и должны. 

let inputRub = document.getElementById('rub'),
    inputUsd = document.getElementById('usd');

inputRub.addEventListener('input', () => {
    let request = new XMLHttpRequest();

    request.open('GET', 'js/currency.json');
    request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    request.send();
    
    request.addEventListener('readystatechange', function() {
        if (request.readyState === 4 && request.status == 200) { 
            let data = JSON.parse(request.response); 

            inputUsd.value = inputRub.value / data.usd;
        } else {
            inputUsd.value = 'Sorry, something went wrong';
        }
    });
}); 

//HTML

<div><input id="rub" type="text" name="rub" placeholder="RUB"></div>
<div><input id="usd" type="text" name="usd" placeholder="USD"></div>
Andrei Subach
2 years ago

Один ответ