AJAX在从iOS提交时未触发PHP邮件发送器。

huangapple go评论149阅读模式
英文:

AJAX is not triggering PHP mailer when submitting from iOS

问题

  1. dataString = 'param=stuff';
  2. jQuery.ajax({
  3. cache: false,
  4. type: "POST",
  5. headers: { "cache-control": "no-cache" },
  6. url: "https://example.com/assets/mailer.php",
  7. data: dataString,
  8. success: window.location.href = "https://example.com/thankyou"
  9. });
英文:
  1. dataString = 'param=stuff';
  2. jQuery.ajax({
  3. cache: false,
  4. type: "POST",
  5. headers: { "cache-control": "no-cache" },
  6. url: "https://example.com/assets/mailer.php",
  7. data: dataString,
  8. success: window.location.href = "https://example.com/thankyou"
  9. });

This works fine with various browsers on Windows, MacOS, and Android. On iOS, the PHP mailer script isn't triggered, but the redirect works. Using Safari dev tools I can see there aren't any errors in the console. What am I missing?

答案1

得分: 1

success接受一个函数,您同时将一个字符串传递给它,并将该字符串分配给window.location.href,然后导航离开。

改用函数代替...

  1. jQuery
  2. .post("https://example.com/assets/mailer.php", { param: "stuff" })
  3. .done(() => {
  4. window.location.href = "https://example.com/thankyou";
  5. });

请注意,我已删除了您设置的所有多余选项,并使用更安全的方法对请求体进行编码。


FYI,您绝对不需要jQuery来实现这个

  1. fetch("https://example.com/assets/mailer.php", {
  2. method: "POST",
  3. body: new URLSearchParams({ param: "stuff" }),
  4. }).then((res) => {
  5. if (res.ok) {
  6. window.location.href = "https://example.com/thankyou";
  7. }
  8. });
英文:

success accepts a function, you are passing it a string while simultaneously assigning that string to window.location.href and navigating away.

Use a function instead...

  1. jQuery
  2. .post("https://example.com/assets/mailer.php", { param: "stuff" })
  3. .done(() => {
  4. window.location.href = "https://example.com/thankyou";
  5. });

Note that I've removed all the redundant options you were setting and used the safer method of encoding the request body.


FYI you definitely don't need jQuery for this

  1. fetch("https://example.com/assets/mailer.php", {
  2. method: "POST",
  3. body: new URLSearchParams({ param: "stuff" }),
  4. }).then((res) => {
  5. if (res.ok) {
  6. window.location.href = "https://example.com/thankyou";
  7. }
  8. });

huangapple
  • 本文由 发表于 2023年3月7日 11:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定