2017-01-05 12 views
0

Ampを使用してフォームをポストし、PHPを使用して処理しています。私は私が直面しています唯一の問題は、そのサイトがアパッチを使用して、ローカルホスト上でホストされているすべてのヘッダーを含める必要がありアンプフォームPHPを使用したPOST

<?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> 

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

コード:私は次のエラーを取得しています。ローカルホストでアンプヘッダーを許可する必要があることを親切に助けてください。

+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

で質問を確認してください。申し訳ありません、ローカルホストで作業しています。 – Stray

1

同じ答えを探しながら、私はあなたの質問を見つけました。リクエストをデバッグしながら、私は次のクエリ文字列が要求

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

に、応答に

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

を追加することによって追加気づいた、AMPは、文句を停止しました。

関連する問題