0

I am new in react gatsby, when i do api call it gives me error origin has been blocked by CORS policy, here i have placed my whole code, can anyone please look into it, and help me to resolve this issue.

API CALL

get_review_list() {
    fetch('http://******/get-dashboard-reviews/1', {
      method: 'POST',
      mode: "cors",
      headers: {
        'Accept': 'application/json',
        'Access-Control-Allow-Origin': '*',
      }
    }).then(function (response) { 
      alert('sdsdsdds'); console.log('123'); 
      //const data = response.json(); console.log(data); 
    })
    .catch(function (error) { console.log(error) });
  }

In gatsby-config.js

proxy: {
    url: "http://********",
},
taks
  • 868
  • 9
  • 31

3 Answers3

1

This means your server doesn't accept your client origin. This is a feature in Web Bowser. (And if you test in postman, it work well).

Cross-Origin Read Blocking (CORB) is an algorithm that can identify and block dubious cross-origin resource loads in web browsers before they reach the web page. CORB reduces the risk of leaking sensitive data by keeping it further from cross-origin web pages.

To resolve this, go to the Back End and accept your client's origin or you can disabled origin policy in Chrome.

To disable origin policy in Chrome please see this post Disable same-origin policy in Chrome

aldenn
  • 1,396
  • 1
  • 4
  • 17
0

The API address you requested must authorize your app URL. For example, if your application is running via example.com, the serverside of API needs a definition like;

Access-Control-Allow-Origin: https://example.com

Access-Control-Allow-Origin

nsei
  • 11
  • 3
-1

I have resolved the issue, simple to add no-cors, thanks to all

get_review_list() {
    fetch('http://******/get-dashboard-reviews/1', {
      method: 'POST',
      mode: "no-cors",
      headers: {
        'Accept': 'application/json',
        'Access-Control-Allow-Origin': '*',
      }
    }).then(function (response) { 
      alert('sdsdsdds'); console.log('123'); 
      //const data = response.json(); console.log(data); 
    })
    .catch(function (error) { console.log(error) });
  }
taks
  • 868
  • 9
  • 31