-1

I am trying to do a GET request with vanilla javascript from a .html, and I am looking to assign the response inside a variable, I have to use the response to manipulate the DOM however, I get a Promise < pending > and not a JSON

This is my code:

let data = fetch('http://localhost/api/products')
        .then(response => response.json())


console.log(data)

I'd like to get this from console.log(data):

{ "name": "SHOES", "price": 129}
Ivandez
  • 249
  • 6
  • 13

1 Answers1

0

this code will be a good guide, you can get json data at then second then

const getCart = () => {
    fetch("/cart.js", {
        method: 'GET',
        headers: new Headers({'content-type': 'application/json', 'X-Requested-With':'xmlhttprequest'})
    }).then(res => {
        return res.json();
    }).then(json => {
        console.log(json);
    });
}
Roman Gavrilov
  • 620
  • 4
  • 17