0

I am new to reactJS. The below snippet is to invoke a URL on a click event. But the URL fired is with the baseURI.

How can i truncate that or reset it ?

handleClick = (event) => {

 console.log("Got the row " + event );

 alert('The link was clicked.'+ event);
    fetch("www.google.com")
    .then(res => {
      console.log("Got the result "+   res.json());
    }
  );

};

The URL fired is

Response:

 {type: "basic", url: "http://localhost:3000/www.google.com", redirected: false, 
   status: 200, ok: true, …}
   (...)

  statusText: "OK"
  type: "basic"
  url: "http://localhost:3000/www.google.com"

I need to fire multiple calls within this handleClick event and then open a new tab if successful with the final URL along with the session key. The above code is just the beginning to check the calls work fine or not.

How should i truncate "http://localhost:3000" ??

sa_n__u
  • 336
  • 1
  • 9
Shaik Md
  • 597
  • 4
  • 8
  • 22

3 Answers3

0

Fetch API doesn't know that it's an external link. Add the protocol to your url, i.e. http:// or https://.

Alserda
  • 4,246
  • 1
  • 17
  • 25
0

Fetching API from external link, you need to add complete URL like

http://google.com

or

https://google.com

Sultan Aslam
  • 5,600
  • 2
  • 38
  • 44
  • its giving exception in chrome https://stackoverflow.com/questions/46522749/how-to-solve-redirect-has-been-blocked-by-cors-policy-no-access-control-allow – Shaik Md Oct 15 '19 at 06:52
  • https://create-react-app.dev/docs/proxying-api-requests-in-development @ShaikMd – Sultan Aslam Oct 15 '19 at 06:58
0

just pass the fetch options:

fetch(URL, {
  mode: 'cors',
  headers: {
    'Access-Control-Allow-Origin':'*'
  }
})

Eslam Abu Hugair
  • 1,162
  • 1
  • 11
  • 23