Hello, I’ve created a simple API using Phoenix, then generated the resource mix phoenix.gen.json User users username:string age:integer email:string
Nothing too fancy, after that, just added the route in the controller and it works like a charm, but then, I try to fetch the users from the api using Angular 2, I’ve already added Corsica to my application and allow requests from everywhere plug Corsica, origins: "*", however I keep getting the
CORS header ‘Access-Control-Allow-Origin’ missing
I’ve already included a header in my requests as you can see here
getUsers() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.get(this.apiUrl + 'users', options)
.map(res => res.json());
}
On the Angular side it seems to be doing the request fine, but the API just says not going to happen.
Anyone experienced the same issue or knows what is a possible solution?
This sounds like you have placed plug Corsica, origins: "*" in the wrong place, or otherwise the VM encountered a runtime error that was handled by Plug.ErrorHandler thereby nullifying the plug pipeline’s modifications to response headers.
Please provide:
The placement of the plug Corsica line
The integer response status code from the server when this error occurs
I’ve placed plug Corsica, origins: "*" in my /lib/projectname/endpoint.ex. I don’t get a response status on my Elixir app, neither running mix phoenix.server nor iex -S mix phoenix.server and in my client I just get
Service ready…
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource … (Reason: CORS header ‘Access-Control-Allow-Origin’ is missing).
Edit: My GET request returns 302 in the network tab, cause xhr, type plain.
If you don’t have a log line showing on your Elixir application, then it’s not receiving a request. So the Angular application is probably hitting some other service that is returning a 302.
But I don’t get why it doesn’t receive the request, I’ve the api running on port 8081 and the Angular app on 8080, if I change port or anything I just get EXCEPTION: Response with status: 0 for URL: null which means I’m hitting the right url, and as I said, doing it to the GitHub api works like a charm.
Edit: Finally got it working, the issue was Cloud9, my workspace was private, which at the time of a request required a login