XMLHttpRequest无法加载http://localhost:3000/get.
XMLHttpRequest cannot load http://localhost:3000/get
我正在从另一个运行在tomcat中的程序中调用node-js服务,当我使用以下代码时,一切都很好。
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path'),
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/get', function (req, res) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
res.send('Hello World!');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
但为了尽量减少代码,我引入了中间件,如下所示,
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path'),
var app = express();
var allowCrossDomain = function(req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(allowCrossDomain);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/get', function (req, res) {
res.send('Hello World!');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
在这一点上,我得到了XMLHttpRequest无法加载http://localhost:3000/get.请求的资源上不存在"Access Control Allow Origin"标头。原点'http://localhost:8083因此不允许访问,我不知道哪里丢了它。
这是我的解决方法,希望对您有所帮助。
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path'),
var app = express();
var allowCrossDomain = function(req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}
// all environments
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(allowCrossDomain);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/get', function (req, res) {
res.end('Hello World!');
});
app.listen(3000,function(){
console.log("It's Started on PORT 3000");
});
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path'),
var app = express();
// all environments
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
app.get('/get', function (req, res) {
res.end('Hello World!');
});
app.listen(3000,function(){
console.log("It's Started on PORT 3000");
});
相关文章:
- PHP中的setcookie仅适用于localhost
- 在localhost Dev Box上测试JSONP请求的最佳方式
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- @Url.Content(“~”)不适用于localhost
- 向localhost发出带有grunt的代理CORS请求
- 岗位http://localhost:3000/api/signup400(错误请求)
- XMLHttpRequest无法加载http://localhost:3000/get.
- 为什么我的JS为我的链接添加前缀(localhost:3000 / mylink)
- 正在为ReactAsync清除localhost:3000个URL
- 错误连接拒绝http://localhost:3000/socket.io/socket.io.js.
- XMLHttpRequest无法加载“;此URL“;起源http://localhost:3000访问控制允许来源不允许
- localhost:3000 dons'切换node.js应用程序时不会更改
- 有人能解释一下为什么(http://localhost:3000/#/home) #出现在url之间吗?
- 重定向localhost而不是myserver.org:3000/login/callback
- 流星应用程序崩溃localhost:3000
- Chrome: http://localhost:3000/ - ERR_CONNECTION_REFUSED
- 热模块更换已启用但不起作用http://localhost:3000/__webpack_hmr.
- 平均堆栈挂起在localhost:3000上
- 为Heroku配置操作电缆-错误localhost:3000/Cable
- GET http://localhost:3000/projects/assets/jquery.masonry.js