【发布时间】:2020-01-31 12:44:27
【问题描述】:
页面在单域/相同域上运行良好,但在多域/跨域的情况下会产生问题。
表单页面
ampform.html
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<title>amp-form</title>
<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>
<link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-form/index.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
</head>
<body>
<h2 class="sample-heading">AMP Form Submission</h2>
<form target="_top" method="post" name="apiForm" id="apiForm" action-xhr="https://different.com/ampl/amp_thankyou.php"
custom-validation-reporting="show-all-on-submit">
<input type="text"
id="as-you-go-name"
name="name"
placeholder="Name..."
required>
<span visible-when-invalid="valueMissing"
validation-for="as-you-go-name"></span>
<input type="submit" name="submitlogin" value="Submit" />
</form>
</body>
</html>
感谢页面
amp_thankyou.php
if(!empty($_POST)){
$domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://www.example.com') .".cdn.ampproject.org");
header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
header("AMP-Redirect-To: https://www.different.com/amp_thankyou.php");
header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin");
echo json_encode(array('successmsg'=>'data post'));
exit;
}
【问题讨论】:
-
...如果出现多域创建问题...不清楚您要问什么
-
表示一个域中的表单页面和另一个域中的操作页面。从不同域调用操作页面
-
请不要只是在这里转储代码 - 问一个实际问题。你到底有什么问题?错误消息清楚地表明响应中没有
Access-Control-Allow-Origin- 那么到目前为止您实际上做了什么来尝试调试它? -
其实我已经添加了上面的代码来避免 CORS 问题。仍然收到山姆错误。我错过了什么