使用Express防止Node.js中的重复POST请求

时间:2016-10-25 18:44:25

标签: javascript node.js

我收到了不受欢迎的重复POST请求。重新加载后第一次提交POST时,它会重复6次,总共7次请求。所有后续POST都会出现两次。这就是它在我的控制台中的显示方式:

User  authenticated, redirecting to admin
GET /admin 304 145ms
GET /static/bootstrap/css/theme/flatly/bootstrap.css 304 1ms
GET /static/bootstrap/css/jumbotron-narrow.css 304 1ms
GET /static/jquery/jquery.js 304 0ms
GET /static/bootstrap/js/bootstrap.min.js 304 0ms
GET /static/images/hppLogo.png 304 0ms
no value entered
POST /remake 408 1ms - 16b
no value entered
POST /remake 408 1ms - 16b
no value entered
POST /remake 408 0ms - 16b
no value entered
POST /remake 408 1ms - 16b
no value entered
POST /remake 408 1ms - 16b
no value entered
POST /remake 408 1ms - 16b
no value entered
POST /remake 408 0ms - 16b
no value entered
POST /team-report 408 0ms - 16b
no value entered
POST /team-report 408 1ms - 16b
no value entered
POST /remake 408 0ms - 16b
no value entered
POST /remake 408 0ms - 16b

我的html中有这个脚本:

script.
        $(document).ready(function () {
            $("#submit").click(function () {
                document.getElementById("submit").disabled = true;
                document.getElementById("submit").innerHTML = "Processing...";
                $.post("/remake", $("#surveyForm").serialize(),
                        function (data) {
                            document.getElementById("submit").disabled = false;
                            document.getElementById("submit").innerHTML = "Generate";
                            document.getElementById("forResults").innerHTML = "Successfully created";
                            document.getElementById("resultsLink").innerHTML = 'samplelink.com' + data.redirect;
                            document.getElementById("resultsLink").href = data.redirect;
                        }
                )
                        .error(function (xhr) {
                            switch (xhr.status) {
                                case 409:
                                    document.getElementById("submit").disabled = false;
                                    document.getElementById("submit").innerHTML = "Generate";
                                    document.getElementById("forResults").innerHTML = "Entry does not exist";
                                    document.getElementById("resultsLink").innerHTML = '';
                                    break;
                                case 408:
                                    document.getElementById("submit").disabled = false;
                                    document.getElementById("submit").innerHTML = "Generate";
                                    document.getElementById("forResults").innerHTML = "Please enter a value";
                                    document.getElementById("resultsLink").innerHTML = '';
                                    break;
                                default:
                                    document.getElementById("submit").disabled = false;
                                    document.getElementById("submit").innerHTML = "Generate";
                                    document.getElementById("forResults").innerHTML = "Failed to create";
                                    document.getElementById("resultsLink").innerHTML = '';
                            }
                        });
            });
        });

表单按钮有type="submit" 并且路线调用此方法:

var createTeamReport = function(teamName, res){
  "use strict";
  var params = {
      TableName: config.STARTUP_SIGNUP_TABLE,
      FilterExpression : 'company = :thisCompany',
      ExpressionAttributeValues: {
          ':thisCompany': {'S': teamName}
      }
  };
  if(teamName !== ''){
      db.scan(params, function(err, data) {
          if (err) {
              console.log('Error scanning DB: ');
              console.log(err);
              res.status(500).send('database error');
          }
          else if (Object.keys(data).length === 0) {
              res.status(409).send('no entries');
          }
          else {
              console.log("Database scan successful");
              res.send();
              jkteamreport.main(data, res);
          }
      });
  }
  else {
      console.log('no value entered');
      res.status(408).send('no value entered');
  }
};

我也在为节点使用dynamoDB和AWS插件。我知道这是一个非常简单的错误,这只是让我疯狂,我无法弄明白

0 个答案:

没有答案