1

我正在尝试显示来自 amp-form 响应的图像。当我在回复时检查 dom 时,amp-img 标签似乎被剥离了。

表单.php

<!doctype html>
<html amp lang="en">
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
</head>
<body>
<form method="post" action-xhr="/amp/test.post.form.endpoint.php">
  <input type="text" name="name" placeholder="Name" required>
  <input type="email" name="email" placeholder="Email" >
  <input type="submit" value="Submit">
  <div submit-success>
    <template type="amp-mustache">

      {{{message}}}
    </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">

      {{message}}
    </template>
  </div>
</form>

</body>
</html>

test.post.form.endpoint.php

<?php

const SUCCESS_CODE = 200;
const SUCCESS_MESSAGE = '<div><p>Start Test<amp-img src="welcome.jpg" alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>';
const ERROR_CODE = 400;
const ERROR_MESSAGE = 'There are some missing values, please review your form.';

$host = isset($_SERVER['HTTP_X_FORWARDED_HOST'])
  ? $_SERVER['HTTP_X_FORWARDED_HOST']
  : $_SERVER['HTTP_HOST'];
header('Content-Type: application/json');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin');
header('AMP-Access-Control-Allow-Source-Origin: https://' . $host);


if (!isset($_POST['name']) || empty($_POST['name'])) {
  $error = [
    'message' => ERROR_MESSAGE
  ];

  $json = json_encode($error);
  http_response_code(ERROR_CODE);
  die($json);
}

$success = [
    'message' => SUCCESS_MESSAGE
];
$json = json_encode($success);
http_response_code(SUCCESS_CODE);
die($json);

?>

导致

<div id="rendered-message-amp-form-0" i-amphtml-rendered=""><p>Start TestEnd Test</p></div>

我怎样才能让 amp-img 标记与其余的 html 代码一起发送,是什么导致它被删除?

4

1 回答 1

0

您的标头内容类型不允许使用标签 header('Content-Type: application/json'); . 请按以下方式更改模板。并且只需返回图像名称作为响应。

<div submit-success>
    <template type="amp-mustache">
<div><p>Start Test<amp-img src={{{ message }}} alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>
   </template>
  </div>
  <div submit-error>
    <template type="amp-mustache">

      <div><p>Start Test<amp-img src={{{ message }}} alt="Welcome" height="400" width="800"></amp-img>End Test</p></div>

    </template>
  </div>
于 2019-07-12T08:00:06.397 回答