jquery ajax没有正确接收node.js响应

时间:2013-03-13 09:13:29

标签: jquery ajax node.js express passport.js

客户端代码:

  for(var i =0; i < 1; i++) {
    void function(item) {
        alert(item);
        $.ajax({
          url : "/documents/ajax/" + item,
          dataType : "text",
          success : function(data) {
            alert("data");
          },
          error: function (xhr, ajaxOptions, thrownError) {
            alert('Error: ' + error.message);
          }
        });
    }(featuredItems.eq(i).text().trim());
  }

Node.js代码:

app.get('/documents/ajax/:id.:format?', function(req, res) {
  console.log("Request Received");
  res.send("Response");
  res.end();

});

使用此代码,我无法在客户端收到回复。

如果我在节点服务器上执行ctrl-c,则会弹出成功警报。

请帮助/

更新:

如果我注释掉以下行,那么我会收到正确的回复:

//  app.use(express.session({secret: 'secret_key'}));
//  app.use(passport.initialize());
//  app.use(passport.session());

1 个答案:

答案 0 :(得分:3)

您正在使用旧式的$ .ajax;阅读http://api.jquery.com/jQuery.ajax/

否则没有post / get方法。

如果您认为快递有问题,只需从终端创建新的清除应用测试express -s myapp

这是我的$ .ajax代码:

$('.form-2').on('submit', function(e) {
    e.preventDefault();
        var data = {};
            data.email = $('#email').val().toLowerCase();
            data.pass = $('#password').val();
        $.ajax({ url: '/login'
               , type: 'POST'
               , data: JSON.stringify(data)
               , contentType: 'application/json'
            })
            .done(function(data) {
                if (data.access == 'logged') ...
            });
});

服务器:

app.post('/login', function(req, res, next) {
    res.json({ access: 'logged' });
});
相关问题