Ad

Response Body Is Null(Fetch Request)

- 1 answer

I am trying to make a request to the bank API, and get its branches in json format.

But I get an empty response. Response

Although, if I insert the link into the browser, I will get the json format.

https://api.privatbank.ua/p24api/pboffice?json&city=Ивано-Франковск

This is how the request works Results

Errors Error

var address = document.getElementById('address').value;

function postData(url = '', data = {}) {
  console.log(url);
  
    return fetch(url, {
        method: 'GET', 
        mode: 'no-cors', 
        headers: {
          "Accept": "application/json",
          'Access-Control-Allow-Origin':'*'
      }
        
    }).then(response => {
      console.log(response);
      if (response.ok)
      {
        response.json()
      }
      else {
        throw new Error('Something went wrong');
      }
    }); 
}

postData(`https://api.privatbank.ua/p24api/pboffice?json&city=${address}`, {})
  .then(data => console.log(JSON.stringify(data))) 
  .catch(error => console.error(error));

`

Ad

Answer

You're executing this as a no-cors request. These are severely restricted and you cannot read the body for it.

See stackoverflow.com/questions/45696999/fetch-unexpected-end-of-input

Also, you're not returning the result of .json(), so your function will in all cases return a promise to undefined.

Ad
source: stackoverflow.com
Ad