我正在将工作从旧环境迁移到新环境。我正在用较新的代码更新过时的代码,它似乎运行良好(我的 Web 服务是用 Node.js 编写的)
我的 web 服务在 node.js 中运行,我的网站使用 jquery。 Node.js代码
var express = require('express');
var app = express();
app.get('/', function(req, res){
console.log('GET request to /');
});
app.listen(8888);
console.log('Express server started on port 8888');
这是我的 jquery 代码
$.getJSON('http://URL:8888/getTables', function(data){
//stuff
});
但是在 firebug 中控制台显示:
"http://URL.com:8888/getTables 200 OK 64ms"
没有任何反应
如果我直接访问该网站 “http://URL.com:8888/getTables” 我得到了我的数据。
有什么想法吗?我将使网络服务保持运行状态,以便您可以看到我在说什么。
正在使用它的网站是 http://www.URL.com/db/index.html
编辑:
app.get('/getTables', function(req, res){
console.log('GET request to /getTables');
getTables(req, res);
sendJSON(res, 200, cache_tables);
});
function sendJSON(res, httpCode, body)
{
var response = JSON.stringify(body);
res.send(response, {'Access-Control-Allow-Origin': '*'}, httpCode);
}
更新
我找到了我的解决方案,感谢您让我知道我的问题是跨域的,我以为我的 sendJSON 已经解决了它,但自从我升级了 nodejs 和模块后,它可能已经过时了。对于那些想知道的人来说,这是修复方法:
//Middleware: Allows cross-domain requests (CORS)
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
//App config
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'secret' }));
app.use(express.methodOverride());
app.use(allowCrossDomain);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
我必须更新我所有的使用方法:
app.get('/', function(req, res, next){
console.log('GET request to /');
});
最佳答案
这是因为您在另一个端口上运行服务器,因此它被视为 x 域。您可以将其设置为使用 JSONP
这个问题更详细How do I send an AJAX request on a different port with jQuery?
这是另一篇文章,详细介绍了 COR http://www.bennadel.com/blog/2327-Cross-Origin-Resource-Sharing-CORS-AJAX-Requests-Between-jQuery-And-Node-js.htm
[更新下面的评论]
我发现一篇文章有一个使用express来提供jsonp的示例设置,希望这会有所帮助......
http://kiwidev.wordpress.com/2011/07/18/node-js-expressjs-and-jsonp-example/
该帖子的代码示例如下:
快捷应用
var express = require('express');
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/logget', function(req, res){
console.log('get to log');
console.log(req.query.callback);
console.log(req.param('somedata'))
res.header('Content-Type', 'application/json');
res.header('Charset', 'utf-8')
res.send(req.query.callback + '({"something": "rather", "more": "pork", "tua": "tara"});');
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
访问它的 HTML 页面
<html>
<head>
<title>jsonp test</title>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#jsonThingLink').click(function(e){
e.preventDefault();
console.log('jsonThingLink clicked');
$.ajax({
dataType: 'jsonp',
data: "somedata=blahblah",
jsonp: 'callback',
url: 'http://localhost:3000/logget?callback=?',
success: function(data) {
console.log('success');
console.log(data);
console.log(data.more);
}
});
});
});
</script>
</head>
<body>
<div id="jsonThing"><a href="#" id="jsonThingLink">test jsonp</a></div>
</body>
</html>
关于jquery getJSON 没有响应,但直接访问有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656300/