0

我正在本地主机上进行测试,并使用模块 CORS 奇怪... POST ajax 调用以获取 access_token (使用模块 node-oauth2-server )成功但随后尝试访问资源,ajax 调用失败( Origin ...Access 不允许-Control-Allow-Origin) 我猜我的路由有问题...

var express = require('express'),
cors = require('cors'),
http = require('http'),
https = require('https'),
fs = require('fs'),
oauthserver = require('node-oauth2-server'),
categories = require('./models/category'),
products = require('./models/product');

var server_options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
cert: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

var app = express();

//app.use(require('cors')());
app.use(require('browser-logger')());

app.use(cors()); // automatically supports pre-flighting
app.use(app.router);

app.configure(function() {
  var oauth = oauthserver({
    model: require('./models/oauth_mongodb'),
     // See below for specification
    grants: ['password', 'refresh_token'],
    accessTokenLifetime: 2592000,  // 30 days
    debug: true
});
app.use(express.bodyParser()); // REQUIRED
app.use(oauth.handler());
app.use(oauth.errorHandler());
});

// categories
app.get('/categories/:id', categories.findById);
app.get('/categories', categories.findAll);

// products
app.get('/categories/:id/products', products.findByCategory);
app.get('/products/:id', products.findById);
app.get('/products', products.findAll);

https.createServer(server_options,app).listen(8000, 'node_ssl_server.local');
http.createServer(app).listen(3000, 'node_ssl_server.local');
4

1 回答 1

1

只是一个猜测,但我认为您的 ajax 调用可能来自:

http://localhost:3000/ 

并指向

https://localhost:8000/

您可能需要刷新浏览器以避免跨域错误。

于 2013-10-16T23:50:43.377 回答