Wordpress表单操作不';我不能用放大器

Wordpress表单操作不';我不能用放大器,wordpress,amp-html,Wordpress,Amp Html,我试图在wordpress中使用AMP和表单动作。我将动作xhr与POST方法结合使用 当我提交表单时,它会给出错误信息 响应必须包含AMP Access Control Allow Source Origin标头 表单提交失败:错误:响应必须包含AMP Access Control Allow Source Origin标头​​​ 甚至尝试添加标题,如下所示 标题(“AMP访问控制允许源代码来源:https://www.demo.com"); 这个问题仍然存在。有什么建议吗 我使用的是wo

我试图在wordpress中使用AMP和表单动作。我将动作xhr与POST方法结合使用

当我提交表单时,它会给出错误信息

  • 响应必须包含AMP Access Control Allow Source Origin标头
  • 表单提交失败:错误:响应必须包含AMP Access Control Allow Source Origin标头​​​
甚至尝试添加标题,如下所示

标题(“AMP访问控制允许源代码来源:https://www.demo.com");

这个问题仍然存在。有什么建议吗


我使用的是wordpress 5.0.3版

您可以使用
.htaccess
文件设置标题

Header set Access-Control-Allow-Origin 'https://cdn.ampproject.org​'
Header set AMP-Access-Control-Allow-Source-Origin 'https://your-domain.com'
或者给你举个简单的例子

function amp_comment_submit(){
  $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
  if ( is_wp_error( $comment ) ) {
    $data = intval( $comment->get_error_data() );
    if ( ! empty( $data ) ) {
      status_header(500);
      wp_send_json(array('msg' => $comment->get_error_message(),
                        'response' => $data, 
                        'back_link' => true ));
    }
  }
  else {
    @header('AMP-Redirect-To: '. get_permalink($_POST['comment_post_ID']));
    @header('AMP-Access-Control-Allow-Source-Origin: ' . $_REQUEST['__amp_source_origin'] );
    wp_send_json(array('success' => true));
  }
}
add_action('wp_ajax_amp_comment_submit', 'amp_comment_submit');
add_action('wp_ajax_nopriv_amp_comment_submit', 'amp_comment_submit');
更多信息:

如果使用官方的WordPress,这些标题将通过该方法自动发送

有关插件行为的更多信息,请参阅剧本