You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gravity Forms API v2自定义表单提交字段映射失败求助

解决Elementor自定义表单AJAX提交到Gravity Forms的字段映射问题

一、明确字段ID的定义

Gravity Forms REST API中input_X里的X,指的是Gravity Forms后台的字段ID,和Elementor表单字段的HTML id/name属性完全无关。

获取该字段ID的方式:

  • 登录WordPress后台,打开目标Gravity Forms表单的编辑页面,每个字段右上角会显示数字ID(比如「字段ID: 3」)
  • 编辑字段时,浏览器地址栏的field_id=X参数里的X就是字段ID
  • 查看Gravity Forms默认渲染的表单代码,input的name属性格式为input_X,这里的X即为所需ID

二、当前代码的排查点

  1. 表单与字段ID匹配
    确认代码里的formID = 15是目标Gravity表单的正确ID(后台表单列表中,每个表单标题下方会显示ID);同时input_1/input_2/input_3必须和Gravity表单里对应字段的ID完全一致,不能随意赋值。

  2. 认证信息正确性
    代码里的btoa("key1:Key2")需要替换为Gravity Forms生成的合法API密钥对:

    • 后台进入「Forms > Settings > REST API」,点击「Add Key」生成Consumer Key和Consumer Secret
    • 格式必须是Consumer Key:Consumer Secret,再用btoa()编码(注意不要加空格)
  3. 数据格式与请求头
    当前代码的input_values结构符合API要求,但要确保:

    • 字段值正确获取:检查console.log(formData)的输出,确认每个input_X对应的值不为空且正确
    • 保留contentType: "application/json",API v2要求JSON格式的请求体

三、WordPress相关必要设置

  1. 启用Gravity Forms REST API v2
    后台「Forms > Settings > REST API」页面,勾选「Enable REST API」,并确保API版本选择「v2」。

  2. 开启Basic Auth支持
    WordPress默认不支持REST API的Basic认证,需二选一操作:

    • 安装并激活WP Basic Auth插件
    • 或者在主题的functions.php中添加以下代码:
      add_filter( 'rest_authentication_errors', function( $result ) {
          if ( ! empty( $result ) ) {
              return $result;
          }
          if ( ! isset( $_SERVER['PHP_AUTH_USER'] ) ) {
              return $result;
          }
          $user = wp_authenticate( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] );
          if ( is_wp_error( $user ) ) {
              return new WP_Error(
                  'rest_authentication_failed',
                  $user->get_error_message(),
                  array( 'status' => 401 )
              );
          }
          wp_set_current_user( $user->ID );
          return $result;
      });
      
  3. 安全插件与服务器配置

    • 若使用Wordfence、iThemes Security等安全插件,需将/wp-json/gf/v2/forms/*/submissions加入API白名单,避免请求被拦截
    • 确认服务器(Apache/Nginx)允许Basic认证:Apache需开启mod_auth_basic模块,Nginx需配置auth_basic相关规则

四、调试技巧

  • 打开浏览器开发者工具的「Network」面板,查看AJAX请求的响应状态码:
    • 401:认证失败,检查API密钥对或Basic Auth设置
    • 404:表单ID错误,确认Gravity表单ID是否正确
    • 400:字段映射错误,检查input_X的字段ID是否匹配,或字段值格式不符合要求
  • 先用Postman等工具手动构造请求,确认API能正常提交数据后,再调整前端代码

修改后的示例代码(假设字段ID匹配)

const formID = 15; // 确认是正确的Gravity表单ID

// 收集表单数据,确保input_X对应Gravity字段ID
const formData = {
  input_1: document.getElementById("name").value, // 假设Gravity姓名字段ID是1
  input_2: document.getElementById("phone").value, // 假设Gravity电话字段ID是2
  input_3: document.getElementById("email").value, // 假设Gravity邮箱字段ID是3
};
console.log(formData);

$.ajax({
  url: "/wp-json/gf/v2/forms/" + formID + "/submissions",
  method: "POST",
  data: JSON.stringify({
    input_values: formData,
  }),
  contentType: "application/json",
  beforeSend: function (xhr) {
    // 替换为你的Consumer Key和Consumer Secret
    const consumerKey = "你的Consumer Key";
    const consumerSecret = "你的Consumer Secret";
    xhr.setRequestHeader("Authorization", "Basic " + btoa(`${consumerKey}:${consumerSecret}`));
  },
  success: function (response) {
    console.log("Form submitted successfully:", response);
    alert("Form submission successful!");
  },
  error: function (error) {
    console.error("Error submitting the form:", error.responseJSON); // 打印详细错误信息
    alert("There was an error submitting the form.");
  },
});

内容的提问来源于stack exchange,提问作者Saad Rizwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:41:18