简体   繁体   中英

Liferay: Call JSON service from portlet

I'm trying to call a json web service with my portlet javascript code: In the js/main.js i have:

   Liferay.Service(
  '/Basic-portlet.hello/remote-hello',
  {
    name: ''
  },
  function(obj) {
    console.log(obj);
  }
);

this call executes only once when I add the portlet to the page, whenever page reloaded a javascript error raised:

Uncaught TypeError: undefined is not a function
A.mix.parseIOConfig
A.mix.parseInvokeArgs
Service    main.js?browserId=other&lan....
(anonymous function)

I've seen this happen when you try to invoke a Liferay JSON web service from a context without access to AlloyUI.

Add the following AUI import:

<script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script>

Then wrap your service invocation like this:

AUI().use('aui-base', function(A){
  // Liferay Service invocation here
});

Eg,

AUI().use('aui-base', function(A){
  Liferay.Service(
    '/user/get-user-by-email-address',
    {
      companyId: Liferay.ThemeDisplay.getCompanyId(),
      emailAddress: 'test@liferay.com'
    },
    function(obj) {
      console.log(obj);
    }
  );
});

I have faced similar issues in past. Try disabling the javascript minifier and see if that solves your issue. It fixed my issue.

Thanks, Gaurav

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM