XMLHttpRequest cannot load [url] Response for preflight is invalid (redirect)

express
ionic
nodejs
mongodb
angular

#1

Edit:

I figured out why I was getting this error: I have my project set to private.

Though this does not solve my problem: I want to keep my data secure, so not everyone can access my calls and make post/delete/edits (unless they are using the app.)

Is there any information on how do add this?

I’m making an ionic app for android and today I implemented a server side nodejs (express) Restful API with mongodb on c9.

But when I trigger a http request to the url, I keep getting the error mentioned in the title:

This is my angular http request on ionic (simply to test first):

app.controller('DashCtrl', function($scope, $http, $q) { 
    $http.get('https://[workspace]-[user].c9users.io/api/capture')
        .then(function(result) {
            console.log(result);
    });
});

This is an image of my workspace:

I used the following code to make the database:
api.js

var Capture = require('../models/capture');
module.exports = function(router) {
    router.get('/capture', function(req, res){
        var capture = new Capture();
        // capture.birdname = req.body.birdname;
        // capture.place.city = req.place.body.city;
        // capture.place.country = req.place.body.country;
        
        capture.birdname = "Pigeon";
        
        capture.save(function(err, data){
            if(err)
                throw err;
            
            res.json(data);
        });
    });
    
    router.get('/captures', function(req, res){
        Customer.find({}, function(err, data){
            res.json(data);
        })
    })
}

capture.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var captureSchema = mongoose.Schema({
    birdname: String,
    place: {
        city: String,
        country: String   
    }
});

module.exports = mongoose.model('Capture', captureSchema)

database.js

module.exports = {
    'url': 'mongodb://' + process.env.IP
}

server.js

var express = require('express');
var app = express();

var mongoose = require('mongoose');
var configDB = require('./server/config/database.js');

mongoose.connect(configDB.url);

var api = express.Router();
require('./server/routes/api')(api);
app.use('/api', api);

app.listen(process.env.PORT, process.env.IP);
console.log('Listening on port ' + process.env.PORT)

Anyone have an idea why I’m getting this error and what I need to do to fix this?
I’m guessing it has something to do with the server allowing me to send requests to the API though I don’t know how to implement that.

Is there also a way to secure my database (API key maybe) and how would I implement this?

Thanks in advance


#2

Found the initial error. Posted a new problem above.


#3

Did you solved it? I have the same problem.

Thanks!


#4

Answer seems to be here