I created a sling servlet skeleton like so...
@SlingServlet(paths = "/bin/foo/bar", methods = "POST")
public class FooBarServlet extends SlingAllMethodsServlet {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setHeader("Content-Type", "text/plain");
response.getWriter().write("foo bar");
LOGGER.info("hello world");
}
}
I created an edit config for my component
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:EditConfig">
<cq:listeners jcr:primaryType="cq:EditListenersConfig"
afteredit="myapp.components.foobar" />
</jcr:root>
I created an cq:ClientLibraryFolder
and added this js to it
var myapp = { components : {} };
myapp.components.foobar = function(component, reloadPage) {
var oncomplete = function(success) {
if (success) {
if (reloadPage) document.location.reload();
else component.refreshSelf();
} else
console.log('could not foobar on component ' + component.path);
};
CQ.HTTP.post('/bin/boo/bar', oncomplete, { path : component.path });
};
My page loads, my component loads, my clientlib js loads, I see no errors in console. I edit my component and hit ok. My servlet is hit, I tail the log server side and see no errors. I see no errors client side when I open the console to trace. My response is 200 ok. Everything looks great! Except that I keep getting an "Unspecified Error" at the top right corner of my browser
Does anyone know where I even begin to troubleshoot this given that I am seeing no errors on the server side log, and no errors on the client side console?
Update
Thanks to @rakhi4110 for the reference to CQ.HTTP. I was able to come up with some stuff from that documentation
First, setting the suppressErrorMsg
flag hid the error message
CQ.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path }, null, true);
Second, I do not like to suppress things, so I tried to craft my response like so
{
"headers" :
{
"Status":200,
"Message":"foo bar"
}
}
However that did nothing.
Third, while looking at the CQ.HTTP api, I noticed that a lot of it was depricated in favor of CQ.shared.HTTP. Simply using the post function from that, without the suppress, worked
CQ.shared.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path });
For now I'm sticking with option #3 until I can figure out the proper json response.