瞭解如何在外部網頁中內嵌最適化表單
您可以在AEM Sites](/docs/experience-manager-64/forms/using/embed-adaptive-form-aem-sites.html?lang=zh-Hant)頁面或AEM外部代管的網頁中,使用[內嵌最適化表單。 內嵌的最適化表單功能完整,使用者可填寫並提交表單,而不需離開頁面。 它可協助使用者保留在網頁上其他元素的上下文中,並同時與表單互動。
在將最適化表單內嵌至外部網站之前,請執行下列步驟:
您可以在網頁中插入幾行JavaScript,以嵌入最適化表單。 程式碼中的API會傳送HTTP要求給AEM伺服器,以取得最適化表單資源,並在指定的表單容器中插入最適化表單。 以下是范常式式碼,可將最適化表單內嵌至外部頁面。 請勿在生產環境中使用程式碼。 自訂程式碼以符合您的網站,例如為使用自己版本jQuery的網站使用iFrame。 使用iFrame有助於避免jQuery版本中的衝突:
將下列程式碼嵌入您網站的網頁:
<!doctype html>
<html>
<head>
<title>This is the title of the webpage!</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="customafsection"/>
<p>This section is replaced with the adaptive form.</p>
<script>
var options = {path:"/content/forms/af/locbasic.html", dataRef:"", themepath:"", CSS_Selector:".customafsection"};
alert(options.path);
var loadAdaptiveForm = function(options){
//alert(options.path);
if(options.path) {
// options.path refers to the publish URL of the adaptive form
// For Example: http:myserver:4503/content/forms/af/ABC, where ABC is the adaptive form
// Note: If AEM server is running on a context path, the adaptive form URL must contain the context path
var path = options.path;
path += "/jcr:content/guideContainer.html";
$.ajax({
url : path ,
type : "GET",
data : {
// Set the wcmmode to be disabled
wcmmode : "disabled"
// Set the data reference, if any
// "dataRef": options.dataRef
// Specify a different theme for the form object
// "themeOverride" : options.themepath
},
async: false,
success: function (data) {
// If jquery is loaded, set the inner html of the container
// If jquery is not loaded, use APIs provided by document to set the inner HTML but these APIs would not evaluate the script tag in HTML as per the HTML5 spec
// For example: document.getElementById().innerHTML
if(window.$ && options.CSS_Selector){
// HTML API of jquery extracts the tags, updates the DOM, and evaluates the code embedded in the script tag.
$(options.CSS_Selector).html(data);
}
},
error: function (data) {
// any error handler
}
});
} else {
if (typeof(console) !== "undefined") {
console.log("Path of Adaptive Form not specified to loadAdaptiveForm");
}
}
}(options);
</script>
</body>
</html>
在內嵌的程式碼中:
options.path
變數的值,並變更最適化表單的發佈URL路徑。 如果AEM伺服器是在內容路徑上執行,請確定URL包含內容路徑。 例如,上述程式碼和adaptive from位於相同的aem表單伺服器上,因此此範例使用adaptive form /content/forms/af/locbasic.html的上下文路徑。options.dataRef
取代為要以URL傳遞的屬性。 您可以使用dataref變數來預填最適化表單。options.themePath
,而非在最適化表單中設定的主題。 或者,您也可以使用request屬性來指定主題路徑。CSS_Selector
是內嵌最適化表單之表單容器的CSS選取器。例如,.customafsection css類是上述範例中的CSS選擇器。最適化表單內嵌在網頁中。 在嵌入式自適應表單中觀察以下內容:
內嵌最適化表單的外部網頁會傳送要求至AEM伺服器,而AEM伺服器通常位於私人網路的防火牆後方。 為確保請求安全地導向至AEM伺服器,建議您設定反向代理伺服器。
讓我們看一個示例,說明如何在沒有調度程式的情況下設定Apache 2.4反向代理伺服器。 在此範例中,您會以/forms
內容路徑來代管AEM伺服器,並映射/forms
做為反向proxy。 它可確保Apache伺服器上的任何/forms
要求都會導向至AEM例項。 此拓撲可協助減少發送器層的規則數,因為所有前置有/forms
的請求都會路由到AEM伺服器。
開啟httpd.conf
配置檔案並取消對以下代碼行的注釋。 或者,您也可以在檔案中新增這些程式碼行。
LoadModule proxy_html_module modules/mod_proxy_html.so
LoadModule proxy_http_module modules/mod_proxy_http.so
在httpd-proxy.conf
配置檔案中添加以下代碼行,以設定代理規則。
ProxyPass /forms https://[AEM_Instance]/forms
ProxyPassReverse /forms https://[AEM_Instance]/forms
在規則中,將[AEM_Instance]
取代為AEM伺服器發佈URL。
如果您未將AEM伺服器載入內容路徑,Apache層的Proxy規則將如下:
ProxyPass /content https://<AEM_Instance>/content
ProxyPass /etc https://<AEM_Instance>/etc
ProxyPass /etc.clientlibs https://<AEM_Instance>/etc.clientlibs
# CSRF Filter
ProxyPass /libs/granite/csrf/token.json https://<AEM_Instance>/libs/granite/csrf/token.json
ProxyPassReverse /etc https://<AEM_Instance>/etc
ProxyPassReverse /etc.clientlibs https://<AEM_Instance>/etc.clientlibs
# written for thank you page and other URL present in AF during redirect
ProxyPassReverse /content https://<AEM_Instance>/content
如果設定了任何其他拓撲,請確保將submit、prefill和其他URL添加到調度程式層的allowlist中。
在網頁中內嵌最適化表單時,請考慮下列最佳實務:
http://[server]:[port]/system/console/configMgr
的AEM Web Console Configuration Manager。