了解如何將最適化表單嵌入外部網頁
您可以在AEM Sites🔗頁面或在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包含內容路徑。 例如,上述程式碼和適用性來源位於相同的aem表單伺服器上,因此此範例使用適用性表單/content/forms/af/locbasic.html的內容路徑。options.dataRef
取代為要以URL傳遞的屬性。 您可以使用dataref變數來預填最適化表單🔗。options.themePath
替換為在適用性表單中設定的主題以外的主題路徑。 或者,您也可以使用請求屬性來指定主題路徑。CSS_Selector
是內嵌適用性表單之表單容器的CSS選取器。例如, .customafsection css類是上述示例中的CSS選擇器。適用性表單已內嵌於網頁中。 在內嵌的最適化表單中觀察下列項目:
內嵌最適化表單的外部網頁會傳送要求至AEM伺服器,而伺服器通常位於私人網路的防火牆後。 為確保將請求安全地導向至AEM伺服器,建議設定反向代理伺服器。
讓我們舉一個範例,說明如何在不使用Dispatcher的情況下設定Apache 2.4反向Proxy伺服器。 在此範例中,您會以/forms
內容路徑托管AEM伺服器,並將/forms
對應至反向代理。 它可確保將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層的代理規則將如下:
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
如果您設定任何其他拓撲,請確定您將提交、預填和其他URL新增至Dispatcher層的允許清單。
將最適化表單內嵌至網頁時,請考量下列最佳實務:
http://[server]:[port]/system/console/configMgr
的AEM Web主控台設定管理器。