Cros problem with my app : how to solve it?


#1

I have a MEAN app and using ionic to make an app version of it.

It will share DB and server with MEAN and ionic app version so basically,
when ionic request something to server it is CORS…

[Error] Failed to load resource: Origin http://192.168.0.4:8100 is not allowed by Access-Control-Allow-Origin. (update_session, line 0)
[Error] XMLHttpRequest cannot load https://cloudtalk.herokuapp.com/api/sessions/update_session. Origin http://192.168.0.4:8100 is not allowed by Access-Control-Allow-Origin. (192.168.0.4, line 0)
[Error] XMLHttpRequest cannot load https://cloudtalk.herokuapp.com/api/posts. Origin http://192.168.0.4:8100 is not allowed by Access-Control-Allow-Origin. (192.168.0.4, line 0)

how should I make it right?


#2

Make sure you have CORS enabled on your server, you can do this by setting headers. These usually do it for me:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,x-prototype-version,x-requested-with');

(that’s PHP though)


#3

Simple Usage (Enable All CORS Requests):

var express = require('express')
  , cors = require('cors')
  , app = express();
 
app.use(cors());
 
app.get('/products/:id', function(req, res, next){
  res.json({msg: 'This is CORS-enabled for all origins!'});
});
 
app.listen(80, function(){
  console.log('CORS-enabled web server listening on port 80');
});

See: https://www.npmjs.com/package/cors