简体   繁体   English

jQuery Ajax无法正确接收到node.js响应

[英]jquery ajax not receiving node.js response properly

Client side code: 客户端代码:

  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 code: Node.js代码:

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

});

With this code I am not able to receive the response on client side. 使用此代码,我无法在客户端收到响应。

If I do a ctrl-c on my node server, then success alert is popped up. 如果在节点服务器上执行ctrl-c,则会弹出成功警报。

Please help/ 请帮忙/

UPDATE: 更新:

If I comment out following lines then I receive response properly: 如果我注释掉以下几行,那么我会收到正确的答复:

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

You are using old style of $.ajax; 您正在使用$ .ajax的旧样式; read http://api.jquery.com/jQuery.ajax/ 阅读http://api.jquery.com/jQuery.ajax/

else no post/get method. 否则没有发布/获取方法。

If you think you have problem with express, just create new clear app from terminal for testing : express -s myapp 如果您认为自己对express有问题,只需在终端上创建新的清晰应用进行测试express -s myapp

here's my $.ajax code: 这是我的$ .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') ...
            });
});

server: 服务器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM