2017-11-30 1 views
0

Ich arbeite an einer vorhandenen statischen Website (nur HTML und Javascript), die AMP verwendet. Ich muss ein Formular hinzufügen, das eine POST-Anfrage an einen Drittanbieter-Dienst übermittelt. Der Dienst akzeptiert nur POST-Anfragen.AMP für eine Formularübermittlung deaktivieren

Wenn ich das Formular mit normalen HTML hinzufügen, erhalte ich den folgenden Fehler.

Only XHR based (via action-xhr attribute) submissions are support for POST requests.

tun, einige der Forschung habe ich gelernt, dass AMP Formen erfordern die action-xhr Attribut. Ändern meines Formulars action Attribut action-xhr Ergebnisse in diesem Fehler:

The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

Gibt es eine Möglichkeit ich ein Formular zu einer Website hinzufügen, die AMP verwendet, so dass das Formular sendet eine POST-Anforderung an einen Dritten URL? Vorzugsweise würde AMP überhaupt nicht stören.

Antwort

0

Hier ist das PHP-Skript Ich verwende eine AMP Form an einen 3rd-Party-Service (JotForm) einreichen:

<?php 

    if (!empty($_POST)) { 
     header("access-control-allow-credentials:true"); 
     header("access-control-allow-headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token"); 
     header("access-control-allow-methods:POST, GET, OPTIONS"); 
     header("access-control-allow-origin:".$_SERVER['HTTP_ORIGIN']); 
     header("access-control-expose-headers:AMP-Access-Control-Allow-Source-Origin"); 
     // change to represent your site's protocol, either http or https 
     header("amp-access-control-allow-source-origin:https://".$_SERVER['HTTP_HOST']); 
     header("Content-Type: application/json"); 
     $firstName = isset($_POST['first_name']) ? $_POST['first_name'] : ''; 
     $lastName = isset($_POST['last_name']) ? $_POST['last_name'] : ''; 
     $phone = isset($_POST['your_phone_number']) ? $_POST['your_phone_number'] : ''; 
     $email = isset($_POST['your_email']) ? $_POST['your_email'] : ''; 
     $city = isset($_POST['driver_app_request_call_hiring_city']) ? $_POST['driver_app_request_call_hiring_city'] : ''; 

     try { 
     include "JotForm.php"; 
     $jotformAPI = new JotForm("APIkey"); 
     $submission = array(
      "3" => $firstName, 
      "4" => $lastName, 
      "5" => $phone, 
      "6" => $email, 
      "7" => $city 
     ); 
     $result = $jotformAPI->createFormSubmission("formId", $submission); 
     $output = ['message' => "Your application was submitted. Thank you!"]; 
     header("Content-Type: application/json"); 
     echo json_encode($output); 

     } catch (Exception $e) { 
     var_dump($e->getMessage()); 
     } 
    } 
?> 

funktioniert ziemlich gut für uns!

+0

Vielen Dank dafür. Leider ist auf der Site keine serverseitige Lösung verfügbar (mit anderen Worten, PHP ist keine Option). Ich habe die Frage aktualisiert, um anzugeben, dass die Site eine statische Site ist (nur HTML und Javascript). – jason

+0

Sie können das PHP-Skript auf jedem Server hosten - es muss nicht auf dieser Site sein. –

Verwandte Themen