2016-04-22 10 views
1

Ich kann einfach nicht Anrufe an meine voll funktionsfähige API machen, weil ich diesen Fehler werden immer -einfach nicht lösen kann CORS Problem

angular.js:9827 **OPTIONS http://xyz.mybluemix.net/add_user** (anonymous function) 
@ angular.js:9827sendReq @ angular.js:9628serverRequest 
@ angular.js:9344processQueue @ angular.js:13189(anonymous function) 
@ angular.js:13205Scope.$eval @ angular.js:14401Scope.$digest 
@ angular.js:14217Scope.$apply @ angular.js:14506(anonymous function) 
@ angular.js:16232completeOutstandingRequest 
@ angular.js:4905(anonymous function) 
@ angular.js:5285 
welcome.html:1 **XMLHttpRequest cannot load http://xyz.mybluemix.net/add_user. 
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:9000' is therefore not allowed access. 
The response had HTTP status code 502.** 

Hier ist der Code bei mir läuft:

//Client side code (Angular JS) 
dataFactory.makeUser(user) 
    .then(function (response) { 
     console.log('User created'); 
     window.location = "index.html"; 
    }, function(error) { 
     console.log('User failed to create: ' + error.message); 
    }); 


    app.factory('dataFactory', ['$http', function($http) { 

     var base = 'xyz'; 
     var dataFactory = {}; 

     //works fine 
     dataFactory.getAllUsers = function() { 
      return $http.get(base+"get_all_users"); 
     }; 

     //works fine 
     dataFactory.getUserById = function (id) { 
      return $http.post(base+"get_user_byId", id); 
     }; 

     //the request with the problem 
     dataFactory.makeUser = function (user) { 
      return $http.post(base+"add_user", user); 
     }; 

     return dataFactory; 
    }]); 


    //Server-side code 


    var express = require('express'); 
    var bodyParser = require('body-parser'); 
    var mysql  = require('mysql'); 
    var app = express(); 
    var cors = require('cors'); 
    app.use(cors()); 


    app.post('/add_user', function(req, res) { 
     var id = req.body.id; 
     var name = req.body.name; 
     var gender = req.body.gender; 
     var email = req.body.email; 
     var age_min = req.body.age_min; 
     var age_max = req.body.age_max; 
     var hometown = req.body.hometown; 
     var picture = req.body.picture; 

     var user = { 
      id: id, 
      name: name, 
      gender: gender, 
      email: email, 
      age_min: age_min, 
      age_max: age_max, 
      hometown: hometown, 
      picture: picture 
     }; 

     connection.query('INSERT INTO users SET ?', user, function(err, rows) { 
      if (err) { 
      res.json(err); 
      return; 
      } 
      res.json(rows); 
     }); 

    }); 

Antwort

3

Es scheint, dass Sie versuchen, einen cor von Ihrer Webdomäne zu localhost zu machen, und Sie haben dem Server localhost nicht ausdrücklich gesagt, dass diese Art von Anfrage in Ordnung ist. Der Server weigert sich, die Hände zu schütteln und kommt zurück (Preflight) und sagt, dass das Geschäft ein No-Go ist.

Ich bin nicht vertraut mit Npm-Cors, und entsprechend der Dokumentation sieht Ihre Implementierung korrekt aus.

Express kommt mit der Fähigkeit, cor zu kontrollieren, das ist, was Sie zu tun versuchen:

app = express(); 
app.use(function(req, res, next) { 
    // 
    //allow cor on origin (use req.headers.origin to allow everything, 
    //very discouraged since you're api only communicates with your site) 
    // 
    res.header('Access-Control-Allow-Origin', 'http://xyz.mybluemix.net'); 

    //if you are passing creds, which you're not 
    //res.header('Access-Control-Allow-Credentials', true); 

    //methods allowed 
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); 

    //headers allowed 
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept'); 

    //continue on with the request 
    next(); 
}); 

ich sicher, dass Ihre Basis http zu machen vergaß zu erwähnen, in den Hafen zu ihm angebracht hat:

$http.post('http://xyz.mybluemix.net:9000/post', data);