利用率的简短示例 CORS 通过客户端JavaScript从外部Web应用程序访问AEM内容。
在此视频中:
/etc/hosts
/etc/hosts
aem-publish.local
to localhost:4503
.有关更多详细信息,请查看 了解AEM中的跨域资源共享(CORS).
此网页的逻辑是
http://aem-publish.local/content/we-retail/.../experience/_jcr_content.1.json
jcr:title
形成JSON响应jcr:title
进入DOM<html>
<head>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body style="width: 960px; margin: 2rem auto; font-size: 2rem;">
<button style="font-size: 2rem;"
data="fn-getTitle">Get Title as JSON from AEM</button>
<pre id="title">The page title AJAX'd in from AEM will injected here</pre>
<script>
$(function() {
/** Get Title as JSON **/
$('body').on('click', '[data="fn-getTitle"]', function(e) {
$.get('http://aem-publish.local/content/we-retail/us/en/experience/_jcr_content.1.json', function(data) {
$('#title').text(data['jcr:title']);
},'json');
e.preventDefault();
return false;
});
});
</script>
</body>
</html>
的OSGi配置工厂 Cross-Origin Resource Sharing 可通过以下方式获取:
http://<host>:<port>/system/console/configMgr > Adobe Granite Cross-Origin Resource Sharing Policy
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
alloworigin="[https://www.example.com:8000]"
alloworiginregexp="[]"
allowedpaths="[/content/we-retail/.*]"
exposedheaders="[]"
maxage="{Long}1800"
supportedheaders="[Origin,Accept,X-Requested-With,Content-Type,
Access-Control-Request-Method,Access-Control-Request-Headers]"
supportedmethods="[GET]"
supportscredentials="{Boolean}false"
/>
要允许缓存内容上缓存和提供CORS标头,请添加以下内容 /clientheaders配置 发布到所有支持的AEM dispatcher.any
文件。
/cache {
...
/clientheaders {
"Access-Control-Allow-Origin"
"Access-Control-Expose-Headers"
"Access-Control-Max-Age"
"Access-Control-Allow-Credentials"
"Access-Control-Allow-Methods"
"Access-Control-Allow-Headers"
}
...
}
重新启动Web服务器应用程序 更改后 dispatcher.any
文件。
可能需要完全清除缓存,以确保在 /clientheaders
配置更新。
Python SimpleHTTPServer (与Windows/macOS/Linux兼容)