Z
Z
zlodiak2017-12-17 19:25:27
JavaScript
zlodiak, 2017-12-17 19:25:27

How to enable Access-Control-Allow-Origin in json-server?

I have an angular2 frontend and json-server backend . angular2 works on address: localhost:4200 json-server works on address: localhost:3000
The problem is that I don't know how to make json-server give data with header: Access-Control-Allow-Origin: *
This is necessary so that the frontend can get the result of a cross-domain query.
Please tell me how to do it. I don't even understand where the server config is located. I didn't find it in node_modules

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
My joy, 2017-12-17
@t-alexashka

// ...
server.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  next()
})
// ...

So you haven't tried it?

A
Anonymous ________________, 2017-12-19
@Onesuch07

I
myself suffer getApiUser() {
let header = new Headers();
header.append('Content-Type', 'application/json; charset=utf-8');
return this.http.get(this.url).map(res => res.json())
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question