-1

I need to know what can I do to solve this problem, which need repair. I do not need to know the problem, need to know the solution. Anyone know how can I do?

my ionic app :

// controller test:
$http({
  method: 'GET',
  url: 'https://example.herokuapp.com/apiDataTest/'
}).then(function successCallback(response) {
    console.log('response'+response);
  }, function errorCallback(response) {
    console.log('response'+response);
  });

my api use express, is this:

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

    app.get('/apiDataTest', getData);

    function getData(req, res) {
        res.json({
            message: 'Ok! Welcome to test data!'
        });
    }

if I call the address by the browser or postman, for example. the json and returned as I need, but if you make the request in is not possible AngularJS due to error 'Access-Control-Allow-Headers'. I saw some questions about it, but did not realize the solution itself, does anyone know where (client or api) and how can I fix to solve?

phdias
  • 205
  • 1
  • 11

1 Answers1

1

Add the following in your API to support CORS:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials");
  res.header("Access-Control-Allow-Credentials", "true");
  next();
});
Josh Slate
  • 774
  • 5
  • 17