Objective: provide some basic data from Express JS to browser requests, all running locally on localhost.
When I connect to the URL (http://localhost/test) with the browser directly I see the data I'm trying to receive in the browser window. However, when I execute the following code in a tag in an HTML document I get "Uncaught (in promise) SyntaxError: Unexpected end of input" and no data retrieved.
fetch('http://localhost/test', {
'content-type': 'application/json',
mode: 'no-cors',
method: 'GET',
redirect: 'follow'
})
.then(response => response.json())
.then(json => console.log(json))
Here's my Express code:
app.get("/test", (req, res) => {
res.header(
"Access-Control-Allow-Origin","http://localhost:*"
)
res.json( {username: 'Joe'})
});
I started without the "res.header" above but after many searches found that as a suggestion. CURL and XMLHttpRequest both work fine.