-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?

1
  • Yea, make your api support CORS. it's easy. research. Commented Jul 20, 2016 at 19:26

1 Answer 1

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();
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.