So the actual solution to your issue turned out to be using jsonp
callback with forecast.io api as they haven't enabled CORS headers for client access. Use $http.jsonp
like this
$http.jsonp(url + lat + ',' + lng + '?callback=JSON_CALLBACK');
In general to enable CORS on your expressjs server do the following.
- Install cors module for express with
npm install cors
- require it
var cors = require('cors');
- Set it on your express app instance with
app.use(cors());
cors
module will automatically add all aors related headers on the response. Alternately you could also configure a lot of options. Check the official docs
OR
If you want to do it yourself you could do the following
var permitCrossDomainRequests = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
// some browsers send a pre-flight OPTIONS request to check if CORS is enabled so you have to also respond to that
if ('OPTIONS' === req.method) {
res.send(200);
}
else {
next();
}
};
then Enable your CORS middleware
app.use(permitCrossDomainRequests);
Enable routes at the end
app.use(app.router);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…