2017-01-05 28 views
0

我使用Amp發表表單並使用PHP進行處理。我收到以下錯誤:使用PHP的Amp表單POST使用PHP

Form submission failed:: Response must contain the AMP-Access-Control-Allow-Source-Origin header

代碼:

<?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的本地主機上託管。請幫我解決我需要做的事情,讓本地主機上的amp頭文件。

+1

你好,歡迎來到Stackoverflow。可悲的是,我們並不知道你的代碼是怎麼樣的。你必須發佈你的代碼,否則我們不能幫你。對於未來的問題,也可以參考http://stackoverflow.com/help/how-to-ask。 – Twinfriends

+0

Twinfriends:我只是在編輯問題,它已經發布了,現在用代碼 – Nony

回答

2

嘗試添加以下行到你的.htaccess:

Header set Access-Control-Allow-Origin "*" 
Header set AMP-Access-Control-Allow-Source-Origin "https://yourdomain.com" 
+0

檢查問題。對不起,只是讀了你在localhost上工作,對此不太確定。 – Stray

1

,我發現你的問題,同時尋找相同的答案。在調試的要求,我注意到下面的查詢字符串添加到請求

?__amp_source_origin=http%3A%2F%2Flocalhost%3A8080 

,並通過添加

header('AMP-Access-Control-Allow-Source-Origin: '.'http://'. $_SERVER['HTTP_HOST']); 

的響應,AMP停止抱怨。