ajax与node.js/express服务器进行GET调用

Val*_*i K 0 ajax node.js express

我正在尝试为node.js写一个小的ajax实时搜索.首先,这是我的Clientside代码:

  $('#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); }
    });
}
Run Code Online (Sandbox Code Playgroud)

这是我的服务器路线:

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');
});
Run Code Online (Sandbox Code Playgroud)

它工作,但我没有收到任何数据.回调函数中的数据始终为null.我做错了什么?thx的帮助

Cri*_*s-O 7

更改

$.ajax('http://127.0.0.1:3000/matchword/' + value + '/', {
Run Code Online (Sandbox Code Playgroud)

$.ajax('/matchword' + value + '/', {
Run Code Online (Sandbox Code Playgroud)