【问题标题】:amp-mustache Success/Error Messages Not Displayingamp-mustache 不显示成功/错误消息
【发布时间】:2019-10-14 00:27:51
【问题描述】:

所以,我上周发布了 this issue 关于重复标题的帖子。我已经解决了这个问题,我确实相信,但现在它又出现了一个我似乎无法解决的新错误。我通过在表单提交函数的 PHP 中删除第二次添加的标题并只允许它们存在于 htaccess 中来解决我的上一个问题。

表单提交 PHP 中存在的唯一标题是将 Content-Type 设置为 application/json

当我提交表单时,会弹出提交消息。然后,当表单提交/失败时,它不会传回 amp-mustache 模板标签中包含的消息。

    <div submitting>
      <template type="amp-mustache">
        Form submitting... Thank you for waiting.
      </template>
    </div>

    <div submit-success>
      <template type="amp-mustache">
        Thanks, your message has been sent successfully.
      </template>
    </div>

    <div submit-error>
      <template type="amp-mustache">
        Unfortunately your message could not be sent. Please try again later.
      </template>
    </div>

环顾四周后,似乎表明我的Content-Type 在提交表单的PHP 上未设置为application/json,但是当我检查Chrome 中的“网络”选项卡时,就标题而言,一切看起来都很好担心。

提交表单的 PHP (xhr-contact-test.php) 响应标头:

access-control-allow-credentials: true
access-control-allow-headers: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token
access-control-allow-methods: POST, GET, OPTIONS
access-control-allow-source-origin: https://www.craigattachments.com
access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin
amp-access-control-allow-source-origin: https://www.craigattachments.com
cache-control: max-age=604800
cf-railgun: 4d68972973 0.01 0.656948 0030 cc99
cf-ray: 4de04cf54e22ccfe-EWR
content-encoding: br
content-security-policy: default-src * data: blob:; img-src * 'self' data: https: https://static.craigmanufacturing.com; script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: https://cdn.ampproject.org/v0.js https://cdn.ampproject.org/v0/ https://cdn.ampproject.org/viewer/ https://cdn.ampproject.org/v/ https://cdn.ampproject.org/rtv/ https://static.craigmanufacturing.com *.google.com https://maps.googleapis.com/maps/api/js/ https://maps.googleapis.com/maps/api/place/js/ https://cdn.polyfill.io https://cdnjs.cloudflare.com/ajax/; object-src 'self' https://www.craigattachments.com/pdf/; media-src 'self' https://www.craigattachments.com/pdf/; plugin-types application/pdf; style-src 'self' 'unsafe-inline' https://static.craigmanufacturing.com https://cdn.ampproject.org/rtv/ https://fonts.googleapis.com/; base-uri 'self';
content-type: application/json
date: Tue, 28 May 2019 12:41:04 GMT
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
expires: Tue, 04 Jun 2019 12:41:03 GMT
feature-policy: microphone 'none'; payment 'none'; geolocation *; sync-xhr 'self' https://www.craigattachments.com
referrer-policy: same-origin
server: cloudflare
status: 200
strict-transport-security: max-age=31536000
vary: User-Agent
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block

它传回的错误如下并指向 amp-form.js 上的第 1073 行,这是在适当的 amp-mustache 模板标签上设置 i-amphtml-rendered 属性的函数。调查这导致我认为内容类型是导致问题的原因,但现在似乎设置正确并且错误仍然存​​在。

Uncaught (in promise) TypeError: b.setAttribute is not a function

我在此链接打开了我的测试联系表:https://www.craigattachments.com/contact-us-test/

对我下一步应该去哪里有什么想法吗?我尝试了一些方法,例如测试某些标头是否存在,在我的 htaccess 中设置 Content-Type 以及仅在 PHP 中设置我的标头。


其他对此挂断的人的解决方案...如果您将电子邮件作为数组回显,例如echo json_encode(array($email));,则会出现此错误。无论如何,从等式中取出数组似乎对我来说是修复它 - echo json_encode($email);


2019/06/12 更新:非常感谢 Aaron。如果将来有人遇到此问题,则已为该问题提供了一些更好的错误处理参数:https://github.com/ampproject/amphtml/pull/22576

【问题讨论】:

    标签: php .htaccess amp-html


    【解决方案1】:

    我快速浏览了一下,看起来从 renderTemplate 方法返回的对象是一个数组,而不是一个元素。

    https://github.com/ampproject/amphtml/blob/master/extensions/amp-form/0.1/amp-form.js#L1121

    代码中可能存在错误。检查 div,看起来它们都是相同的成功消息。是否可以重现这是一个更简单的形式演示,只需重现最低限度。例如只是形式 谢谢

    【讨论】:

    • 这是精简版:craigattachments.com/test-form.php。对不起,我花了这么长时间,午餐不在。
    • 感谢 Craig,我可以使用您的 JSON 发布数据集重现它,它看起来是一个错误,我将提交一个问题并考虑修复它。今天晚些时候将更新更多细节。谢谢
    • 提交了一个问题github.com/ampproject/amphtml/issues/22570。看起来您无意中碰到了一个永远不能与 amp-form 一起使用的代码路径。您是否有理由返回带有 post 端点的 JSON 数组? craigattachments.com/xhr-contact-test.php 改变这将解决问题......虽然很明显我们会解决它。感谢演示。
    • 老实说,不完全确定为什么我们通过 JSON 数组返回它。我没有参与表单的基本设置,只是帮助发送它,现在我正在努力浏览网站以修复一些问题。这周我在工作中扮演了大约 3 个额外的角色,但我下周会去挖掘,看看我是否能想出一个更好的答案。感谢您提交问题,我订阅了它以了解其进展情况。
    • 所以,我不知道为什么它会作为 JSON 数组传回。我删除了阵列,现在一切都很好。 echo json_encode(array($email_subject, $email_address, $html_email, $email_name)) 变为 echo json_encode($html_email),我的错误消息似乎正常运行。
    猜你喜欢
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多