在 Safari/Firefox iOS 上無法傳送表單數據
P粉520204081
P粉520204081 2024-04-04 12:53:48
0
1
482

這適用於除 iOS 上的 Safari/Firefox 之外的所有平台:

jQuery("#gform_1").one('submit', function(event) {
  const form = document.getElementById("gform_1");
  const firstName = document.getElementById('input_1_1').value;
  const lastName = document.getElementById('input_1_2').value;
  const phoneNumber = document.getElementById('input_1_3').value;
  const email = document.getElementById('input_1_4').value;

  const formData = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
  };

  jQuery.ajax({
    url: 'https://www.example.com/submissions.php',
    type: 'POST',
    data: formData,
    success: function(response) {
      console.log("Success!", response);
    },
    error: function(xhr, status, error) {
      console.error("Error!", error);
      console.log(xhr.responseText);
    }
  });
});

console.error("錯誤!", error) 為空。它說“錯誤!”。 console.log(xhr.responseText) 也是空白的。我不認為這是 COR 問題,因為 Submits.php 與表單託管在同一伺服器/網域上。

P粉520204081
P粉520204081

全部回覆(1)
P粉340264283

我需要防止Default。

jQuery("#gform_1").one('submit', async function(event) {
  event.preventDefault();
  const form = document.getElementById("gform_1");
  const firstName = document.getElementById('input_1_1').value;
  const lastName = document.getElementById('input_1_2').value;
  const phoneNumber = document.getElementById('input_1_3').value;
  const email = document.getElementById('input_1_4').value;

  const formData = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
  };
  const request = await new Promise((resolve, reject) => {
    jQuery.ajax({
      url: 'https://www.example.com/submissions.php',
      type: 'POST',
      data: formData,
      success: function(response) {
        console.log("Success!", response);
        resolve();
      },
      error: function(xhr, status, error) {
        console.error("Error!", error);
        console.log(xhr.responseText);
        reject(error);
      }
    });
  });
  await request;
  console.log("AJAX request completed!");
});
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板