2

When I send a HTTP Get request to this web uri via Postman, the response is returned, but when I send a request like below, I get a cors error. ?

const getData = async () => {
const url = " https://www.aponet.de/apotheke/apothekensuche?tx_aponetpharmacy_search%5Baction%5D=result&tx_aponetpharmacy_search%5Bcontroller%5D=Search&tx_aponetpharmacy_search%5Bsearch%5D%5Bplzort%5D=rothenburg&tx_aponetpharmacy_search%5Bsearch%5D%5Bstreet%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Bradius%5D=10&tx_aponetpharmacy_search%5Bsearch%5D%5Blat%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Blng%5D=&type=1981 "
         
axios.get(url)
  .then((response) => {
    console.log(response);
  })
  .catch((err) => {
    console.log(err);
  })
}
naveen
  • 53,448
  • 46
  • 161
  • 251
  • this is because, the server hasn't enabled CORS. "Postman is not a browser, so is not limited by CORS policy" – naveen Oct 24 '22 at 08:05
  • so what should i do to get cors error? – ali yıldız Oct 24 '22 at 08:09
  • That has to be done by the team at "https://www.aponet.de". They get to set the CORS policy. You get to access resource via the browser, if the policy allow you to do so. – naveen Oct 24 '22 at 08:14

1 Answers1

-1

Actually, postman offers "Code Snippet" functions for developers to obtain the API fetch code directly after testing. It locates on the right of the window with icon "</>".

Here is the code provided by postman.

var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

fetch("https://www.aponet.de/apotheke/apothekensuche?tx_aponetpharmacy_search%5Baction%5D=result&tx_aponetpharmacy_search%5Bcontroller%5D=Search&tx_aponetpharmacy_search%5Bsearch%5D%5Bplzort%5D=rothenburg&tx_aponetpharmacy_search%5Bsearch%5D%5Bstreet%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Bradius%5D=10&tx_aponetpharmacy_search%5Bsearch%5D%5Blat%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Blng%5D=&type=1981", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

For axios

var config = {
  method: 'get',
  url: 'https://www.aponet.de/apotheke/apothekensuche?tx_aponetpharmacy_search%5Baction%5D=result&tx_aponetpharmacy_search%5Bcontroller%5D=Search&tx_aponetpharmacy_search%5Bsearch%5D%5Bplzort%5D=rothenburg&tx_aponetpharmacy_search%5Bsearch%5D%5Bstreet%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Bradius%5D=10&tx_aponetpharmacy_search%5Bsearch%5D%5Blat%5D=&tx_aponetpharmacy_search%5Bsearch%5D%5Blng%5D=&type=1981',
  headers: { }
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});
kiuQ
  • 931
  • 14
  • 1
    Not an answer. It still gives "CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource" error. – naveen Oct 24 '22 at 08:01
  • Not an answer. i keep getting the error. – ali yıldız Oct 24 '22 at 08:07
  • May I ask that where do you call this fetch function? Through a browser? Or from your website which the domain is not "www.aponet.de" ? – kiuQ Oct 24 '22 at 08:16
  • I'm trying with the axios package in the react project. – ali yıldız Oct 24 '22 at 08:19
  • Since I tried the code above in react-native app project and it works. And I remember that you cannot simply call API from other domain website. It will blocked by CORS policy. – kiuQ Oct 24 '22 at 08:22