I open a static page from the file system which tries to fetch a resource from a server running on localhost ( namely a directory listing of / ). The browser refuses to forward this request and suggests:
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Then I submit the request using 'no-cors':
let body=JSON.stringify({action:"listfiles",path:"/"})
let headers = new Headers()
headers.append("Content-Type", "application/json");
fetch('http://localhost:9000/ajax',{
method: 'POST',
headers: headers,
body: body,
mode: 'no-cors'
}).then(
response=>response.json()
).then(
data=>this.onload(data)
)
On the server side I use express in the following way:
app.use('/ajax',bodyParser.json())
app.post("/ajax",function(req,res){
let action=req.body.action
console.log(`ajax ${action}`)
}
This time the browser lets the request through, and even app.post
gets the request, just the request body is not parsed correctly, instead of 'listfiles'
I get undefined
value for action
. ( The same problem does not occur if I load the page from localhost ).