XmlHttpRequest和AEM Servlet

时间:2017-10-03 18:00:18

标签: xmlhttprequest aem

我正在客户端进行原始ajax调用,如下所示,

var url = '/bin/denisa/dummyServlet';
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');
xhr.onload = function() {
 if (xhr.status === 200) {
alert('deni');

 }
   else {
   alert('Request failed. Returned status of ' + xhr.status);
  }
};
xhr.send();

到AEM中的以下servlet:

@Component(
   service = {
   Servlet.class
   },
   property = {
   "sling.servlet.paths=/bin/denisa/dummyServlet",
   "sling.servlet.extensions=json",
   "sling.servlet.methods=GET"
   }
)
public class DummyServlet extends SlingSafeMethodsServlet {

   private static final Logger LOG = LoggerFactory.getLogger(DummyServlet .class);

   @Reference
   private dummyService dummyService;

   @Override
   protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

   Gson gson = new Gson();
   JsonElement jsonElement = gson.toJsonTree(dummyService);
   response.setContentType("application/json");
   response.getWriter().write(gson.toJson(jsonElement));
  }
}

我得到了404,我不知道为什么,因为路径是相同的,也是contentTypes。有人有线索吗?

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好,似乎配置端缺少了

你可以登录felix控制台吗? http://localhost:4502/system/console/bundles

展开项目包并检查您的servlet 服务ID xx类型:javax.servlet.Servlet和其他详细信息 *还要确保未安装捆绑包并处于活动状态

然后转到http://localhost:4502/system/console/configMgr并验证Apache Sling Servlet /脚本解析器执行路径是否包含/ bin / entry

如果上面提到的配置导航到http://localhost:4502/system/console/servletresolver 检查servlet响应。

这些是帮助我解决404

的一些配置