留言板

Call JSON remote services of custom services

Marsin Alexandre,修改在12 年前。

Call JSON remote services of custom services

New Member 帖子: 13 加入日期: 10-3-10 最近的帖子
Hello,

I've created a custom plugin service via Service Builder on Liferay portal 6.0.6.
The service works fine when I use it server side (TestLocalServiceUtil).
Remote-service is set to True.

I can see the WSDL of my service via this URL : http://127.0.0.1:8080/TestService-portlet/axis

I can also call JSON methods of my service via this URL : http://127.0.0.1:8080/TestService-portlet/secure/json?serviceClassName=com.test.service.TestServiceUtil&serviceMethodName=findByName&serviceParameters=[name]&name=Test

But I can't get to use the JSON remote service from javascript, I see many different ways to use liferay services in ajax from a portlet (JQuery, AUI, ...) but none of these works.
In a first time, I just want to feed a combobox asynchronoulsy via my custom service.

Anyone can provide me a working example of the best pratice to call liferay custom service from portlet in Ajax ?

Thanks by advance
Marsin Alexandre,修改在12 年前。

RE: Call JSON remote services of custom services

New Member 帖子: 13 加入日期: 10-3-10 最近的帖子
Hello again,

To be more specific I can use the Liferay Services like CountryService :

JSP :

<script src="/html/js/liferay/service.js" type="text/javascript"> </script>

<aui:script use="liferay-dynamic-select,aui-io-request">
                new Liferay.DynamicSelect(
                    [
                        {
                            select: "<portlet:namespace />countryId",
                            selectData: function(callback) {
											Liferay.Service.Portal.Country.getCountries(
												{
													active : true
												},
												callback
											);
										},
                            selectDesc: 'name',
                            selectId: 'countryId',
                            selectVal: ''
                        }
                    ]
                );
</aui:script>


For my custom service I have to add this code to avoid undefined fonction : (Is there a way to include the custom service.js ?)

<script type="text/javascript">
Liferay.Service.register("Liferay.Service.loc", "com.test.service");
Liferay.Service.registerClass(
	Liferay.Service.loc, "Test",
	{
		findByName: true
	}
);
</script>


Then I call my service the same way :


<aui:script use="liferay-dynamic-select,aui-io-request">

                new Liferay.DynamicSelect(
                    [
                        {
                            select: "<portlet:namespace />test",
                            selectData: function(callback) {
											Liferay.Service.loc.Test.findByName(
												{
													name : 'test'
												},
												callback
											);
										},
                            selectDesc: 'Test_Label',
                            selectId: 'Test_Code',
                            selectVal: ''
                        }
                    ]
                );
    </aui:script>


But this time I get a 'undefined' in my combobox.
The problem must be my service, but how can I test it in javascript?

Thanks
Marsin Alexandre,修改在12 年前。

RE: Call JSON remote services of custom services

New Member 帖子: 13 加入日期: 10-3-10 最近的帖子
After many research, I found that the exception :

On the ajax request, the TestServiceUtil is called, then it calls the TestServiceImpl (Where the remote methods are defined).
So it's ok, but when I look the content of the HTTP response I got this :

{"exception":"com.test.model.TestClp.toJSONArray(java.util.List)"}

So I looked in the TestClp model, there is no toJSONArray() methods, it is in TestJSONSerializer class.
Why do I got this exception while it's working when I call the URL (http://127.0.0.1:8080/TestService-portlet/secure/json?serviceClassName=com.test.service.TestServiceUtil&serviceMethodName=findByName&serviceParameters=[name]&name=Test)


By the way I've used this fix (http://issues.liferay.com/browse/LPS-13073), perhaps it's related.

Any help appreciated