Solvedangular what's wrong with the http headers in angular 2 using typescript
✔️Accepted Answer
CORS doesn't come into play when using curl - it is only browser's mechanism. At this is a mechanism on a browser level, not a framework level. More info: http://www.html5rocks.com/en/tutorials/cors/
Other Answers:
Please use @crossorigin on the backend side in Java Spring boot controller (either class level or method level) as the solution for Chrome error'No 'Access-Control-Allow-Origin' header is present on the requested resource.'
This solution is working for me 100% ...
Example : Class level
@crossorigin
@controller
public class UploadController {
----- OR -------
Example : Method level
@crossorigin(origins = "http://localhost:3000", maxAge = 3600)
@RequestMapping(value = "/loadAllCars")
@responsebody
public List loadAllCars() {
Ref: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
I had the same issue,
client side --> angular 5
server side --> Nodejs v9.8.0
I have added cors to the server app
npm install cors --save
then imported:
const cors = require('cors');
Then enabled the middleware :
app.use(cors());
Thats it, now it is working fine with no issue.
In my experience the plugins worked with http but not with the latest httpClient. Also, configuring the CORS respsonse headers on the server wasn't really an option. So, I created a proxy.conf.json file to act as a proxy server.
Read more about this here: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md
below is my proxy.conf.json file
{
"/posts": {
"target": "https://example.com",
"secure": true,
"pathRewrite": {
"^/posts": ""
},
"changeOrigin": true
}
}
I placed the proxy.conf.json file right next the the package.json file in the same directory
then I modified the start command in the package.json file like below
"start": "ng serve --proxy-config proxy.conf.json"
now, the http call from my app component is as follows
return this._http.get('/posts/pictures?method=GetPictures')
.subscribe((returnedStuff) => {
console.log(returnedStuff);
});
Lastly to run my app, I'd have to use npm start or ng serve --proxy-config proxy.conf.json
is adding @crossorigin works
I found several documents telling me to set the headers by the code below.
when I look at the console when sending this request, the request headers look like:
I can find "token" in Access-Control-Request-Headers, but, where is my "somevalue"?,
by using "curl" command, the request headers look like the code below and it works well.
Also having this header problem, I receive the error message all the time even though I set the "w.Header().Set("Access-Control-Allow-Origin", "*")" in my server side.(no error occurs if I don't add headers in the request)