【问题标题】:amp-form post action-xhr response stripping image tagamp-form post action-xhr 响应剥离图像标签
【发布时间】:2019-11-21 21:43:44
【问题描述】:

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

form.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 代码一起发送,是什么导致它被删除?

【问题讨论】:

    标签: javascript php json xmlhttprequest amp-form


    【解决方案1】:

    您的标头内容类型不允许使用标签 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>
    

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 1970-01-01
      • 2014-10-09
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多