0

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));
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Roni_Droni
  • 35
  • 2
  • 7

2 Answers2

0

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.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
0

I think you are trying to fetch data using no-cors when the site doesn't provide right headers then your code will not be able to access the response a proper explanation can be seen in how to process fetch response from an 'opaque' type?

jaswanth
  • 515
  • 2
  • 7