No value when doing a jquery.get () on a json file

0

I have an api that returns a json whose format is as follows:

{
"id": 255,
"texto": "En un lugar de la mancha",
"categories": []
}

From an HTML I try to get one of the multiple phrases of the API in the phrase variable with the following script and then replace the headers with the phrase.

<script>
jQuery.get("direccionapi", (Response) => {
    var frase = Response.texto;
    $('h2').text(frase);
})
</script>

I do not get any type of result does not change the value of the header h2. But if I look at the firefox console it appears to me that it has done the GET. In "Response payload" appears the json of the api.

Thanks for the help

    
asked by Luis 09.02.2018 в 18:24
source

1 answer

0

The arrow function = > returns an array of elements. You could access the first element but it is not recommended, so the other option is to use the common function

jQuery.get("direccionapi", function(Response) {
    var frase = Response.texto;
    $('h2').text(frase);
});

Greetings!

    
answered by 09.02.2018 в 19:35