Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AJAX POST Method Call error : javax.jcr.nodetype.ConstraintViolationException

Avatar

Level 2
I have a high chart which displays JSON data. I want to export these JSON data to Excel file and that logic is there in JSP file. I can use the JQuery AJAX-GET call to pass the JSON data to JSP page and display these data in output Stream of excel type (response.setContentType("application/octet-stream")) which is working fine. My question is, Passing JSON data in URL is very lengthy and wants to Use the POST method Like below example(POST CALL CODE). When I use this example, I get exception as "javax.jcr.nodetype.ConstraintViolationException: No matching property definition: json = {"asset":"XLS","assetType":"Growth","fundName":"Growth portfolio"}"
 POST CALL CODE                $.ajax({         url: url, type: 'POST', data: {json: JSON.stringify({ asset: "XLS", assetType: "Growth", fundName: "Growth portfolio" })}, dataType: 'json', success: function(msg){ alert("worked"); } });

 

 

Why do I need to define "json" property in CQ ? How do we resolve this issue ? Appreciate your help !

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

looking at the code snippet, you try to post a JSON body, but the Default PostServlet is handling it. This expects that the body is in a key-value format, and the key is the name of a property. So it tries to create a property "json:", which is interpreted with "json" being a JCR namespace. So you are trying to create a property with a non-registered namespace.

So please don't POST a JSON string, but rather simple form-like data. This will work fine then.

Jörg

View solution in original post

9 Replies

Avatar

Level 10

Why do I need to define "json" property in CQ ?

There is no difference in Posting JSON data to an AEM Sling Servlet as opposed to other web technologies - such as servlet running on a J2EE App. 

See: 

http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-fr...

Now when you do post JSON data to an AEM Sling Servlet - make sure that the SLing Servlet contains Java app logic to handle the data and parse it. 

http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java

For information on how to build a Sling Servlet and post data to it - see this article:

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

You can try to modify the code in this article to pass JSON data to the servlet and then use Java logic on the servlet to parse the JSON.

Also - we have a community article that shows you how to use Excel Java API in an OSGi bundle that should help you in your Excel requirements: 

https://helpx.adobe.com/experience-manager/using/aem-reporting-service.html

In this example - notice that the OSGi bundle creates and writes data to Excel. 

Avatar

Employee Advisor

Hi,

can you post the POST body of your AJAX request?

Jörg

Avatar

Level 2

I was trying to get the Passed data value in JSP file as request.getParameter("json");

Looks like I need to create Sling Servlet and define the json property there and then call this AJAX Post call. I am going to check that.

 

Thanks.

Avatar

Administrator

Hi 

Please have a look at these reference old forum post having same kind of problem:-

Link:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

// 

        Do you have an OSGI bundle that has the servlet for your form defined? Or are you using the OOB CQ form?

        If you are using the OOB CQ form, what action are you trying to do?

        If you have an OSGI bundle for the Servlet logic, go to http://localhost:4502/system/console and view the bundles associated with your CQ instance listed there.

        Find your OSGI bundle in the list and see if it is running (running bundles will have a 'stop' button beside them). If it is not running, try force starting it.

        If that does not work, there must be some issues with how you had built the bundle (Issues with your maven project if you use one to build the OSGI bundle).

 

Link:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

Link:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

 

I hope this will help you a bit.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Correct answer by
Employee Advisor

Hi,

looking at the code snippet, you try to post a JSON body, but the Default PostServlet is handling it. This expects that the body is in a key-value format, and the key is the name of a property. So it tries to create a property "json:", which is interpreted with "json" being a JCR namespace. So you are trying to create a property with a non-registered namespace.

So please don't POST a JSON string, but rather simple form-like data. This will work fine then.

Jörg

Avatar

Level 9

Have you tried posting the same data into unstructured data node?. Just a guess, it might work. An Unstructured node should accept any kind of property.

Avatar

Administrator

Hi

Can you please look at the answers posted and let us know if this worked for you or not.

If you have devised another way to resolve the issue, please post it in the community for communities benefit.

 

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Employee Advisor

Hi,

it doesn't matter to which kind of nodetype you are adding a property to. If the property name contains a namespace, and the namespace isn't there, JCR tries to create the namespace for you. But if you don't have enough permissions to do so, it will throw this kind of exception.

Jörg

Avatar

Level 2

I changed the current logic and sending JSON file name/path in GET parameter and reading the JSON file on server side. 

So for me this handled the requirement. Thanks for valuable suggestion. This forum is very helpful.