【发布时间】:2013-11-06 19:34:21
【问题描述】:
我正在尝试为 node.js 编写一个小的 ajax 实时搜索。首先是我的客户端代码:
$('#words').bind('keyup', function(){
getMatchingWords($('#words').val(), function (data){
console.log('recieved data');
console.log(data);
$('#ajaxresults').show();
});
});
function getMatchingWords(value, callback) {
$.ajax('http://127.0.0.1:3000/matchword/' + value + '/', {
type: 'GET',
dataType: 'json',
success: function(data) { if ( callback ) callback(data); },
error : function() { if ( callback ) callback(null); }
});
}
这是我的服务器端路由:
app.get('/matchword/:value', function(req, res) {
console.log(req.params.value);
res.writeHead(200, {'content-type': 'text/json' });
res.write( JSON.stringify({ test : 'test'}) );
res.end('\n');
});
它有效,但我没有收到任何数据。回调函数中的数据始终为空。所以我做错了什么?谢谢你的帮助
【问题讨论】:
-
不确定这是否对 node.js 有影响,但
content-type不应该是Content-Type?