【发布时间】:2017-01-05 09:08:39
【问题描述】:
我正在使用 Amp 发布表单并使用 PHP 进行处理。我收到以下错误:
表单提交失败:: 响应必须包含 AMP-Access-Control-Allow-Source-Origin 标头
代码:
<?php
if(isset($_POST\['submitlogin'\]))
{
$name = isset($_POST\['name'\]) ? $_POST\['name'\] : '' ;
$output = \[
'name' => $name
\];
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("AMP-Same-Origin: true");
header("Access-Control-Allow-Origin: *.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: *");
#header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
echo json_encode($output);
die();
}
?>
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<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.1.js">
</script>
<link rel="canonical" href="localhost">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<title>AMP form</title>
</head>
<body>
<form method="post" action-xhr="#" target="_top">
Name:<input type="text" name="name" />
<input type="submit" name="submitlogin" value="Submit" />
</form>
<div submit-success>
<template type="amp-mustache">
Success! Thanks for trying the
<code>amp-form</code> demo! The name submited was {{name}}
</template>
</div>
</body>enter code here
</html>
我已包含所有标题,我面临的唯一问题是该站点使用 Apache 托管在 localhost 上。请帮助我完成我需要做的事情,允许本地主机上的 amp 标头。
【问题讨论】:
-
您好,欢迎来到 Stackoverflow。可悲的是,我们不是知道您的代码长什么样的巫师。您必须发布您的代码,否则我们无法帮助您。也可以看看这个stackoverflow.com/help/how-to-ask 以了解未来的问题。
-
Twinfriends : 我只是在编辑问题,它已经在路上发布了,现在用代码检查问题