【问题标题】:Netlify-forms Contact Form submission 404 errorNetlify-forms 联系表单提交 404 错误
【发布时间】:2020-12-25 01:29:02
【问题描述】:

我无法通过使用 Netlify-Forms 提交我的联系页面,我收到 404 错误。

我有以下表格:-

<form name="Portfolio_Contact" 
      method="POST" 
      data-netlify="true"
      data-netlify-honeypot="bot-field"
      action="/thank-you">
    <div className="form-group">
      <input
        type="text"
        name="name"
        placeholder="name"
        className="form-control"
      />
      <input
        type="email"
        placeholder="email"
        name="email"
        className="form-control"
      />
      <textarea
        name="message"
        rows="5"
        placeholder="message"
        className="form-control"
      ></textarea>
      <div data-netlify-recaptcha="true"></div>
    </div>
    <button type="submit" className="submit-btn btn">
      send me your message
    </button>
  </form>

当我提交它时,我首先收到 404 错误,然后按预期获得我的感谢页面。

我做错了吗?我可以在表单部分看到我的“Portfolio_Contact”。

感谢您的帮助和时间。

【问题讨论】:

    标签: reactjs gatsby netlify netlify-form


    【解决方案1】:

    您有一个 reCAPTCHA 集,但您的表单并不期待它,因为您的 &lt;form&gt; 标记中没有定义它。只需添加:

    <form name="Portfolio_Contact" 
          method="POST" 
          data-netlify="true"
          data-netlify-honeypot="bot-field" 
          data-netlify-recaptcha="true"
          action="/thank-you">
    

    发现data-netlify-recaptcha="true"。您可以在Netlify documentation for reCAPTCHA 2找到更多信息。

    我在网络上得到这个:- Referrer Policy: strict-origin-when-cross-origin

    除了&lt;form&gt; 标签上的这个配置,您还需要设置POST 操作配置。 Netlify 的回复有点奇怪,404 并不意味着您的表单不存在,而是意味着请求失败。我通常应用我发布的确切 &lt;form&gt; 配置,但我添加了一个自定义提交句柄函数来设置请求配置:

    <form name="Portfolio_Contact" 
          method="POST" 
          data-netlify="true"
          data-netlify-honeypot="bot-field" 
          data-netlify-recaptcha="true"
          action="/thank-you">
        <div className="form-group">
          <input
            type="text"
            name="name"
            placeholder="name"
            className="form-control"
          />
          <input
            type="email"
            placeholder="email"
            name="email"
            className="form-control"
          />
          <textarea
            name="message"
            rows="5"
            placeholder="message"
            className="form-control"
          ></textarea>
          <div data-netlify-recaptcha="true"></div>
        </div>
        <button type="submit" className="submit-btn btn" onClick={handleSubmit}>
          send me your message
        </button>
      </form>
    

    您的handleSubmit 函数:

      const handleSubmit = event => {
        event.preventDefault();
    
       // do your verifications and checks
       if(!verified) return false    
    
        const REQUEST_PARAMETERS = {
          method: `POST`,
          headers: { 'Content-Type': `application/x-www-form-urlencoded` },
          body: encode({ ...data }), //your data here. Needs to have your form-name attribute set
        };
    
        fetch(`/`, REQUEST_PARAMETERS)
          .then(() => {
            console.log(`OK`);
          })
          .catch(error => alert(error));
      };
    

    注意:您的请求在您的本地环境中不起作用

    此配置很重要,因为设置了请求的标头,即您的示例中失败的原因。

    您可以在此DEV article 中查看样本。

    【讨论】:

    • 不幸的是无法正常工作。我还删除了 data-netlify-honeypot="bot-field" 使其看起来像示例但一点运气都没有
    • 我在网络中得到这个:- 推荐人政策:strict-origin-when-cross-origin
    • 我将它添加到表单 enctype="multipart/form-data" 和 ,现在我得到了303 响应
    • 好的,这是由于您的 AJAX 请求的配置所致。我会更新我的答案。
    • 嗨 Ferran,我从
      标签中删除了 data-netlify-recaptcha="true" 并且它起作用了。我当然没有得到验证码,但是我会用谷歌做一个,有一个例子。这是我让它工作的唯一方法!
    猜你喜欢
    • 2018-12-24
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多