dataString = 'param=stuff';
jQuery.ajax({
cache: false,
type: "POST",
headers: { "cache-control": "no-cache" },
url: "https://example.com/assets/mailer.php",
data: dataString,
success: window.location.href = "https://example.com/thankyou"
});
这适用于 Windows、MacOS 和 Android 上的各种浏览器。在 iOS 上,不会触发 PHP 邮件程序脚本,但重定向有效。使用 Safari 开发工具,我可以看到控制台中没有任何错误。我错过了什么?
success
接受一个函数,您正在向它传递一个字符串,同时将该字符串分配给 window.location.href
并导航离开。
改用函数...
jQuery
.post("https://example.com/assets/mailer.php", { param: "stuff" })
.done(() => {
window.location.href = "https://example.com/thankyou";
});
请注意,我已经删除了您设置的所有冗余选项,并使用了更安全的编码请求正文的方法。
仅供参考,您绝对为此不需要 jQuery
fetch("https://example.com/assets/mailer.php", {
method: "POST",
body: new URLSearchParams({ param: "stuff" }),
}).then((res) => {
if (res.ok) {
window.location.href = "https://example.com/thankyou";
}
});