我正在嘗試使用Reference Link來實施AMP通訊訂閱表。一旦提交表單時,在服務器端我用下面的代碼來處理請求並返回響應:AMP通訊表單回覆問題
服務器端腳本:
<?php
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: https://www.example.com");
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
$data = array('name'=>$_POST['name'],'email'=>$_POST['email']);
echo json_encode($data);exit;
?>
AMP FORM HTML
<form method="post"
class="p2"
action-xhr="https://www.example.com/request.php"
target="_top">
<div class="ampstart-input inline-block relative m0 p0 mb3">
<input type="text"
class="block border-none p0 m0"
name="name"
placeholder="Name..."
required>
<input type="email"
class="block border-none p0 m0"
name="email"
placeholder="Email..."
required>
</div>
<input type="submit"
value="Subscribe"
class="ampstart-btn caps">
<div submit-success>
<template type="amp-mustache">
Success! Thanks {{name}} for trying the
<code>amp-form</code> demo! Try to insert the word "error" as a name input in the form to see how
<code>amp-form</code> handles errors.
</template>
</div>
<div submit-error>
<template type="amp-mustache">
Error! Thanks {{name}} for trying the
<code>amp-form</code> demo with an error response.
</template>
</div>
</form>
一旦請求完成。它始終顯示我的HTML表單模板的submit-success
部分。我的主要問題是如何顯示submit-error
上述表格的一部分,並且從服務器端返回name
,以及如何在服務器端處理請求以便它可以分別顯示success
或error
消息?
謝謝@SebastianBenz!給我提示錯誤狀態,即4XX或5XX。不過,我閱讀了文檔,但我無法理解。但現在它清楚。謝謝+1) –