Foros de discusión

How to call a Liferay service builder method from javascript

thumbnail
Mario R, modificado hace 9 años.

How to call a Liferay service builder method from javascript

Junior Member Mensajes: 55 Fecha de incorporación: 6/06/13 Mensajes recientes
Hello everyone,
I created a service using liferay service builder (Liferay Portal CE 6.2 ga3)
And I want to consume this service using a javascript code from a content template.
The code used is as follows.


<div class="hero-unit">
	<div id="shipping">
		<p><select id="persona" name="persona"><option value="">Choose a name..</option> </select></p>
	</div>
</div>

<script type="text/javascript">
AUI().use("aui-base", function(A) {
     Liferay.Service(
  		'/PORTAL_INFOS_DATA-portlet.persona/find-all',
	  	function(obj) {
     			for (i = 0; i < obj.length; i++) {
              			A.one('#persona').append('<option value="' + obj[i].nombre + '">' + obj[i].nombre + ' - '+ obj[i].apellido + 			'</option>');
            		}
  		}
	);
});
</script>


When i see the content looged in, the records are shows fine.
But, when i run the content not logged in, the content does not show information and occurs an authentication error in the javascript code.

I put on my portal-ext. properties the following settings, but unfortunately does not work.

json.service.auth.token.hosts.allowed=127.0.0.1
json.service.auth.token.enabled=false
jsonws.web.service.public.methods=*
jsonws.servlet.hosts.allowed=127.0.0.1
auth.token.ignore.origins=\
        com.liferay.portal.jsonwebservice.JSONWebServiceServiceAction:/PORTAL_INFOS_DATA-portlet.persona/find-all,\
        com.liferay.portal.jsonwebservice.JSONWebServiceServiceAction:/PORTAL_INFOS_DATA-portlet.persona/find-persona-by-id,\


I've checked the forums and examples and I could not find a working example or proper guidance.
Possible scenarios may be, have the services without authentication or pass credentials via script.
The liferary service builder portlet has only public read only methods.

Thanks for your help...

Atte:
Mario
thumbnail
Víctor Ponz, modificado hace 8 años.

RE: How to call a Liferay service builder method from javascript

New Member Mensajes: 18 Fecha de incorporación: 29/07/14 Mensajes recientes
Hello Mario.

I don't know the answer to this question, but you can try another approach.
I've made a portlet that returns a json object that it is consumed in javascript code. The skeleton I've used is from sample-json-portlet and I've changed the method getContent from SampleJSONServlet.java to adapt it to my needs.
In javascript I use this code
	YUI().use('node', 'io', 'aui-io-request', 'json-parse', 'overlay', 'aui-form-validator', function (Y) {
		Y.on('domready', function () {
function _getJSON(){
					url = "/PORTAL_INFOS_DATA-portlet/servlet/do?id=<portlet:namespace />json";
					url += "&amp;plid=" + Liferay.ThemeDisplay.getPlid();
					........
					Y.io.request(
					url,
						{
							on: {
								failure: function(){alert("ERROR")},
								success: function(event, id, obj) {
									if (responseData) {
										Your code...
									else {
										alert("error");
									}
								}
							}
						}
					);

I hope this helps
thumbnail
Mario R, modificado hace 8 años.

RE: How to call a Liferay service builder method from javascript

Junior Member Mensajes: 55 Fecha de incorporación: 6/06/13 Mensajes recientes
Thanks for your reply..

I'll try that way!!

Atte:
Mario