我前段时间发现了一个片段,通过ajax将我的表单发布到我的PHP文件中。 在我的开发机器上,一切都运行良好,但是当我移动到我的生产网站空间时,我遇到了ajax请求状态的问题。 将发送请求并禁用完整表单,但它永远不会跳转到失败,完成或始终。
我尝试升级jquery版本,我也检查过文件本身是否有错误,但是请求告诉我,它发布在我网站空间的正确文件中。
以下是摘录
// Variable to hold request
var request;
// Bind to the submit event of our form
$("#form").submit(function (event) {
// Prevent default posting of form - put here to work in case of errors
event.preventDefault();
// 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);
// Fire off the request to /form.php
request = $.ajax({
url: "postFile.php",
type: "post",
data: serializedData
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR) {
// Log a message to the console
console.log("done");
});
// 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 occurred: " +
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);
});
});