【发布时间】:2016-04-19 17:59:59
【问题描述】:
在你说这是重复之前,请听我说完。我遵循了这个答案 (jQuery Ajax POST example with PHP),但我正在尝试将数据发送到 Google 表格。我已经在 Google 表格上运行了代码,因为我可以通过直接从表格中运行我的代码来向其中添加数据。
但是,让它与我的网页一起工作显然是个问题。尽管我已经按照上面发布的问题的答案进行了操作,但我认为这与我缺乏在该领域工作的经验有关。
下面的代码是一个浮动页脚,包含在整个网页的代码中(其中 jquery-1.11.0.min.js 和 jquery-migrate-1.2.1.min.js 已经被调用)。当我点击提交按钮时,页面看起来像是在处理请求,但 Google 工作表 Sheet1 (https://docs.google.com/spreadsheets/d/19l2kSHdBKEWtIFX44FxLBdvCoKjy7VqPF4IW6C1xAZc/edit?usp=sharing) 上从未出现任何内容。
#document
<html>
<body>
<div class="floater-footer" id="the-floater-footer">
<span id="myTestSpan"></span>
<div class="row">
<div class="col-md-1 col-sm-2 col-xs-3"><p>Newsletter</p></div>
<div class="col-md-8 col-sm-7 col-xs-9">
<form id="floater_footer" class="validate subscribe_form">
<div id="subscrive_group wow fadeInUp">
<!-- <input type="email" value="" name="EMAIL" class="form-control subscribe_mail" id="mce-EMAIL" placeholder="email address" required /> -->
<input type="text" class="form-control subscribe_mail" id="bar" name="bar" value="" placeholder="email address" required />
<!-- <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="subscr_btn subscribe_btn"> -->
<input type="submit" value="Subscribe" class="subscr_btn subscribe_btn" />
</div>
</form>
</div>
<div class="col-md-3 col-sm-3 hidden-xs">
<div class="pull-right">
<!-- social media icons here -->
</div>
</div>
</div>
</div>
<script type="text/javascipt">
jQuery( document ).ready(function( $ ) {
// variable to hold request
var request;
// bind to the submit event of our form
$("#floater_footer").submit(function(event){
// abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// serialize the data in the form
var serializedData = $form.serialize();
// let's disable the inputs for the duration of the ajax request
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
//$('#result').text('Sending data...');
// fire off the request to /form.php
request = $.ajax({
url: "https://script.google.com/macros/s/AKfycbyQIDmSInumcrNmU4zxIa4pV8tIlN3A9zx5L5o1hH4qNdP9nDw/exec",
type: "post",
data: serializedData
});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// log a message to the console
//$('#result').html('<a href="https://docs.google.com/spreadsheets/d/10tt64TiALYhPMqR2fh9JzkuhxW7oC0rXXPb_pmJ7HAY/edit?usp=sharing" target="_blank">Success - see Google Sheet</a>');
console.log("Hooray, it worked!");
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
// callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// reenable the inputs
$inputs.prop("disabled", false);
});
// prevent default posting of form
event.preventDefault();
});
});
</script>
</body>
</html>
对可能出现的问题有什么建议吗?与本网站上的大多数人相比,我在 Web 开发方面非常缺乏经验。所以我敢肯定这是我缺少的一些简单的东西!
【问题讨论】:
-
你看过浏览器开发者工具中的AJAX请求/响应吗?您是否在项目中包含了 jQuery 库?是否报告了错误?你是在网络服务器上运行它吗?
-
我没有在开发者工具中看到这个。我不知道该怎么做,因为 Chrome 中的元素、控制台和网络标签是我唯一使用的。我包括 jquery-1.11.0.min.js 和 jquery-migrate-1.2.1.min.js。报告的唯一错误与此问题无关,它们并不是我现在关心的真正错误。它实际上是在实时站点上运行的。上面的流量不多,所以我直接用它测试。
-
这些是开发者工具的一部分。
-
如果它发出 AJAX 请求,您在页面重新加载时就会丢失该请求。如果它正在重新加载页面,则您遇到
event.preventDefault();的问题 -
你可以做到,很高兴你解决了。
标签: javascript php jquery html google-spreadsheet-api