AMP表单即使在响应代码为200时也始终显示提交错误

时间:2018-09-26 21:36:50

标签: go amp-html

我正在使用AMP表单将数据发送到我的Golang服务器。即使我跳过对发送数据的所有处理,只是按照AMP表单参考中的代码200编写响应,我仍然会看到提交错误模板。

这是我的表格(我跳过了字段,因为它太长了)

<form action-xhr="/contactus" method="POST" class="contactForm" target="_top" custom-validation-reporting="show-all-on-submit" id="contactForm">
    <fieldset>
        <!-- divs with input and submit button -->
    </fieldset>
    <div submit-success>
        <template type="amp-mustache">
            Success! 
        </template>
    </div>
    <div submit-error>
        <template type="amp-mustache">
            Error! 
        </template>
    </div>
</form>

这是服务器响应的示例:

HTTP/1.1 200 OK
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sun, 23 Sep 2018 21:48:15 GMT
Content-Length: 23
Content-Type: application/x-gzip

问题出在服务器端吗? 我不知道...任何想法都很感激

2 个答案:

答案 0 :(得分:0)

我很尴尬。 经过几天的思考并回到这个问题上,当我最终决定发布一个问题时,一个想法在我做完后立刻就打动了我。

问题出在标头Content-type上。必须将其设置为JSON:

Content-type: application/json

答案 1 :(得分:0)

确保实现AMP CORS标头。

引用:https://amp.dev/documentation/guides-and-tutorials/learn/amp-caches-and-cors/amp-cors-requests?referrer=ampproject.org

如果请求是针对您自己的来源(您的域而不是amp缓存的服务器),则可以设置以下标头-

if (req.headers['amp-same-origin'] === 'true') {
    origin = req.query.__amp_source_origin;
    sourceOrigin = origin;
}

res.set({
    'Access-Control-Allow-Origin': origin,
    'AMP-Access-Control-Allow-Source-Origin': sourceOrigin,
    'Access-Control-Allow-Source-Origin': 'AMP-Access-Control-Allow-Source-Origin',
    'Access-Control-Expose-Headers': 'Access-Control-Allow-Origin' + ', AMP-Access-Control-Allow-Source-Origin' + ', Access-Control-Allow-Source-Origin'
});
相关问题