单击调试多个Google表单提交?

时间:2017-09-21 13:03:41

标签: javascript jquery ajax forms google-form

我正在尝试使用Google电子表格构建一种购买请求应用。有一段时间,(就像我一直在研究的那样),我的代码正在运行。对于订单中的每一行,它将遍历值,填写Google表单输入,提交表单,然后再次启动该过程。

昨天我注意到每次提交两行提交第一行一次,第二行提交两次,第三行提交三次,依此类推。然后它就停止了提交。然后它再次开始提交,然后停止。你们可以看看我的代码并告诉我我做错了吗?

function formSubmit() {

  //Create unique ID (number of milliseconds since 1/1/70)
  var d = new Date();
  var n = d.getTime();
  var uniqueID = n.toString();

  //Loop through the lines of the order, fill in the values, submit
  $('.orderline').each(function(i, obj) {

    //Stop the default redirect so we can submit multiple times
    $('#ss-form').submit(function(e) {
      e.preventDefault();
      $.ajax({
        url: "https://docs.google.com/a/vt.edu/forms/d/e/1FAIpQLSf77MuDLeqyPbuDCBcpVagi6-hdiUpgZtr0CbuJ3kO-vXPswg/formResponse",
          data: $(this).serialize(),
          type: "POST",
          dataType: "jsonp",
          success: function(data) {
                console.log("Submission successful");
          },
          error: function(xhr, status, error) {
                console.log("Submission failed: "  + error);
          },
        });
      });

    $("#entry_1725077600").val(uniqueID);

    var name = $("#personname").val();
    var email = $("#personemail").val();

    $("#entry_1352722479").val(name);
    $("#entry_1024015951").val(email);

    //etc.

    $("#ss-form").submit();

  });

如果你们想看一下,表格是公开的。注意我在同一次点击时一次提交两张表格;上面的那个是订单中的项目,第二个是关于订单的元数据。

编辑:formSubmit()是从第二个将文件上传到Google云端硬盘的功能调用的(如果有更好的方法可以让我知道):

if(document.getElementById('fUpload').value!='') {

        var user = gapi.auth2.getAuthInstance().currentUser.get();
        var oauthToken = user.getAuthResponse().access_token;

        var uploadObj = $("[id$=fUpload]");
        var file = uploadObj.prop("files")[0];
        var metadata = {
            'title': file.name,
            'description': " ",
            'mimeType': file.type || 'application/octet-stream',
            "parents": [{
                "kind": "drive#file",
                "id": "0B5zM5ktmwJ2fN0c3RWYxWC1rUzQ"
            }]
        };
        var arrayBufferView = new Uint8Array(file);
        var uploadData = new Blob(arrayBufferView, {type: file.type || 'application/octet-stream'});

        try{
            var uploader =new MediaUploader({
                file: file,
                token: oauthToken,
                metadata: metadata,
                params: {
                    convert:false,
                    ocr: false
                }
            });
            uploader.upload();
        } catch(exc){
            showErrorMessage("Error: " + exc);
            $("#fUpload").val(" ");
        }
    } else {
        formSubmit();
    }
});

然后成功回复:

MediaUploader.prototype.onContentUploadSuccess_ = function (e) {
    if (e.target.status == 200 || e.target.status == 201) {

        var response = e.target.response; //Get the response body
        var parsed = JSON.parse(response); //Parse the response body to JS object
        var fileID = parsed.id; //Get the file ID from the response
        var linkToFile = "https://drive.google.com/open?id=" + fileID;

        $("#entry_1703377267").val(linkToFile); //Add the file ID as the value of the file ID input field
        formSubmit(); //Run the rest of the form submit functions

        this.onComplete(e.target.response);

    } else if (e.target.status == 308) {
        this.extractRange_(e.target);
        this.retryHandler.reset();
        this.sendFile_();
    }
};

编辑2:我从未在控制台中看到成功或错误消息。此外,看起来事情没有被提交两次,它们以模式提交:第一项一次,第二项两次,第三项三次等。

0 个答案:

没有答案