angularjs - Bluemix Cors disable Node.js -


i not able avoid error on browser - xmlhttprequest cannot load https://xyz.mybluemix.net/get_user_byid.response preflight request doesn't pass access control check: no 'access-control-allow-origin' header present on requested resource. origin 'http://a.x.y.z:9000' therefore not allowed access. response had http status code 404.

i using every measure required allow cross origin calls, still gives me error.

var app = express(); var cors = require('cors'); app.use(cors());  app.use(function(request, response, next) {     response.header("access-control-allow-origin", "*");     response.header("access-control-allow-headers", "origin, x-requested-with, content-type, accept");     response.header("access-control-allow-methods", "get, post, put");     next(); });  app.get('/get_users', cors(), function(req, res) {...} 

i calling api - (standard $http angular way)

return $http.post(base+"get_user_byid", id) 

you need only:

var cors = require('cors'); //importing middleware enables cors 

and

app.use(cors()); 

to allow cors in express/connect applications

delete these lines:

app.use(function(request, response, next) {     response.header("access-control-allow-origin", "*");     response.header("access-control-allow-headers", "origin, x-requested-with, content-type, accept");     response.header("access-control-allow-methods", "get, post, put");     next(); }); 

and set api with:

app.get('/get_users', function(req, res) {...} 

in angularjs have call api method (no post) because have defined /get_users api method.

i suggest use snippet:

      var par = {         id: myid,       };      var app = angular.module('myapp', []);       app.controller('myctrl', function($scope, $http) {          $http({             method : "get",             params: par,             url : base + "/get_users"         }).then(function mysucces(response) {             $scope.mydata = response.data;         }, function myerror(response) {             $scope.mydata = response.statustext;         });  }); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -