Nikhil Tomy
Nikhil Tomy

Reputation: 73

Why fetch() request fails in chrome console when URL is empty ? . But Works fine in Firefox

When I try to do this on chrome browser:

fetch('https://jsonplaceholder.typicode.com/users')
.then(response=> response.json())
.then(user=> console.log(user))

it seems net::ERR_FAILEDenter image description here

Upvotes: 2

Views: 1310

Answers (1)

machineghost
machineghost

Reputation: 35797

As @Sergiu Paraschiv noted in the comments, the core issue is CORS, and I highly recommend reading the link he provided.

However, just to elaborate slightly, for your specific case, of trying to make an AJAX request on the "start" or "no URL" page of a browser ...

... different browsers handle such "home page AJAX requests" differently, because such a page doesn't have a true URL.

Chrome (evidently) treats it as a URL of "", and so any URL would by definition violate CORS. In contrast, Firefox seems to ignore the CORS URL restrictions on requests made from the "home page".

More generally, if you're going to be testing your API without a client, I'd highly recommend using an independent request-making tool like Postman, as such tools will completely bypass CORS restrictions (and also let you make POST, DELETE, and other non-GET requests).

Upvotes: 5

Related Questions