CQ5 component afteredit

I created a skeleton servlet sling like this ...

@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 editing configuration 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 cq:ClientLibraryFolderand 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 is loading, my components are loading, my clientlib js is loading, I do not see errors in the console. I edit my component and click OK. My servlet hit, I take the server side of the logs and see no errors. I see no errors on the client side when I open the console for tracking. My answer is 200 ok. All perfectly! Except that I keep getting "Unspecified Error" in the upper right corner of the browser

Unspecified Error

Does anyone know where I am even starting to fix this problem, given that I see no errors in the log on the server side and no errors on the client side?

Update

@rakhi4110 CQ.HTTP. -

, suppressErrorMsg

CQ.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path }, null, true);

, , :

{
    "headers" :
    {
         "Status":200,
         "Message":"foo bar"
    }
}

.

, CQ.HTTP api, , CQ.shared.HTTP. post , ,

CQ.shared.HTTP.post('/bin/foo/bar', oncomplete, { path : component.path });

№ 3, json.

+4
3

Unspecified error , CQ.HTTP.post()

. , .

, suppressErrorMsg . .,

CQ.HTTP.post('/bin/boo/bar', oncomplete, { path : component.path }, null, true);

, , ,

  • : HTML
  • HTML- "". .

HTML, ,

<html>
<head>
    <title>OK</title>
</head>
<body>
<h1>OK</h1>
<table>
    <tbody>
        <tr>
            <td>Status</td>
            <td><div id="Status">200</div></td>
        </tr>
        <tr>
            <td>Message</td>
            <td><div id="Message">Demo Notification Message</div></td>
        </tr>
    </tbody>
</table>
</body>
</html>

. CQ.HTTP

+2
@Properties({
    @Property(name = "sling.servlet.extensions", value = "json"),
    @Property(name = "sling.servlet.methods", value = "POST"),
    @Property(name = "service.description", value = " foo bar Servlet")
}

@Override
    protected final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
        throws ServletException, IOException {
        final String value = request.getRequestParameter("value").getString();
            // create response

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        try {
            final JsonGenerator generator = FACTORY.createJsonGenerator(response.getWriter());

            MAPPER.writeValue(generator, Collections.singletonMap("Message", "success"));
        } catch (final JsonGenerationException jge) {
            LOG.error("error generating JSON response", jge);
        } catch (final JsonMappingException jme) {
            LOG.error("error mapping JSON response", jme);
        } catch (final IOException ioe) {
            LOG.error("error writing JSON response", ioe);
        }
    }

-

postfunction="function(value) {
    var dialog = this.findParentByType('dialog');
    var url = CQ.HTTP.addParameter(dialog.path + '.json', 'value', value);
    var result = CQ.HTTP.eval(url);
    return result;
}
0

sotot sling :

HtmlResponse → HTML

JSONResponse → JSON

Everyone inherits from https://sling.apache.org/apidocs/sling7/org/apache/sling/servlets/post/AbstractPostResponse.html and you can use one of the onXXX methods to record your changes.

Example:

JSONResponse result = new JSONResponse();
result.setStatus(200, "Content changed");
result.onChange;
result.onCopied;
result.onCreated;
result.onDeleted;
result.onModified;
result.onMoved;
boolean setStatus = true;

result.send(slingHttpServletResponse, setStatus);
-1
source

All Articles