get file in JSON format of request XMLHttpRequest with a JavaScript Promise

0

I have the following call to an API through a Promise, do I need to get the result of the call in a JSON instead of text, in order to work with it, as it would be possible? thanks!

$(document).ready(() => {

    const c = (x) => console.log(x);

    function consultarApi(requestUrl){
        return new Promise( resolve =>{
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function(){
                if (this.readyState == 4 && this.status == 200){
                    resolve(this.responseText);
                }
            };
            xhttp.open("GET", requestUrl, true);
            xhttp.send();
        });
    } 

    async function traerApi(_requesUrl){
        try {
            var profile = await consultarApi(_requesUrl);
            // c('Profile : ');
            c(profile);
        } catch (error){
            c(error);
        }
    }

    traerApi('https://api.coinmarketcap.com/v2/ticker/1/');



});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
asked by Gabriela 08.09.2018 в 04:32
source

1 answer

0

I have been able to achieve it with the method parse() of the following way;

resolve(JSON.parse(this.responseText));

ready, I get the answer in JSON

format

thanks!

    
answered by 08.09.2018 в 05:18