【问题标题】:ajax get request in node js expressajax在节点js express中获取请求
【发布时间】:2015-09-15 02:49:38
【问题描述】:

我正在学习我的 Node js 技能。

我想通过单击按钮向 mongodb 添加一些数据。

客户端代码如下所示:

        $(function() {
        $('#add').click(function(){
            $.ajax({
                type: "GET",
                url: "/item/<%= products._id %>/add"
            }).done (function (data) {
                alert(data);
                console.log(data);
            });
        });
    });

  <button type="submit" id="add" class="btn btn-primary">Interessted</button>

这样的服务器端代码:

    app.get('/item/:id/add', function(req, res) {

    Listing.findByIdAndUpdate(
        { _id : req.params.id},
        { $push : {"product.interessteduser": req.user._id }},
        {  safe: true, upsert: true},
        function(err, model) {
            if(err){
                console.log(err);
            }

        });
});

代码非常适合我,但如果我稍等片刻,我会在控制台中收到另一个请求。

看起来像这样:

GET /item/557eec02aa1046b805190207/add 200 120001ms
GET /item/557eeb82aa1046b805190206/add 200 120000ms

所以每次请求 /item/:id/add 并等待 120000 毫秒时,我都会收到另一个请求。如何阻止这种情况?

我想在执行 /item/557eeb82aa1046b805190206/add 获取请求后点击按钮,仅此而已。

【问题讨论】:

    标签: jquery ajax node.js mongodb


    【解决方案1】:

    您从不响应请求。如果您没有响应,浏览器会在两分钟后重试请求。尝试类似

    res.send("success");
    

    【讨论】:

      【解决方案2】:

      然而,当您得到答案时,编写单独的函数来处理发送成功/失败案例总是很优雅的,这样一旦测试就应该使用它并避免这些错误。


        --Expose these two helper methods as module and always use that module to send the result or error response at last.
      
          exports.send_success = function (res, data) {
              res.writeHead(200, {"Content-Type": "application/json"});
              var output = { error: null, data: data };
              res.end(JSON.stringify(output));
          };
      
          exports.send_failure = function (res, err) {
              var code = exports.http_code_for_error(err);
              res.writeHead(code, { "Content-Type" : "application/json" });
              res.end(exports.error_for_resp(err));
          };
      
          function (err, results) {
                  if (err) {
                      helper.send_failure(res, err);
                  } else {
                      helper.send_success(res, results);
                  }
          }
      

      【讨论】:

        【解决方案3】:

        res.end();return false; 添加到您的代码中

        试试这样的

            app.get('/item/:id/add', function(req, res) {
        
            Listing.findByIdAndUpdate(
                { _id : req.params.id},
                { $push : {"product.interessteduser": req.user._id }},
                {  safe: true, upsert: true},
                function(err, model) {
                    if(err){
                        console.log(err);
                    }else{
                     res.json({'status':'200','message':'Success!'});
                     res.end();
                     return false;
                     }
        
                });
           });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-24
          • 1970-01-01
          • 2020-09-18
          • 1970-01-01
          • 1970-01-01
          • 2013-04-11
          • 1970-01-01
          相关资源
          最近更新 更多