Invoking REST service that returns JSON from worklight

In the Invoking REST service from WorkLight i built a simple WorkLight application that makes REST call but the REST service in that case returns XML, To me if your building a REST service for access from Browser it would be much better idea to return JSON instead of XML that avoids one conversion. So with that goal in mind i wanted to change my application and i followed these steps for that
  • First thing that i had to do was change the REST service so that it returns JSON and also i made JSON as the default return type for service, you can get more information and download the sample application by using entry
  • Next i changed the ContactRESTService-impl.js, the worklight REST adapter that i am using for making the actual call,
    
    function getContactList() {
      var input = {
          method : 'get',
          returnedContentType : 'json',
          path : '/ManageContact/rest/contact'
      };
      return WL.Server.invokeHttp(input);
    }
    
    function searchContact(lastName){
      var input = {
            method : 'get',
            returnedContentType : 'json',
            path : '/ManageContact/rest/contact/search?lastName='+lastName
        };
        return WL.Server.invokeHttp(input);
    }
    
    Only change in this file is to let the WorkLight adapter know that i am expecting json as response type instead of xml
  • After that i had to change the way i am parsing the response and using it to display results to the user, the reason being now the structure of the response is different so the callback that reads the response and displays the result has to know about how to handle the changed structure.
    
    function loadContactSuccess(result) {
      console.log("Inside loadContactSuccess " + result);
      var html = '';
      try {
        if (result.status == 200) {
          var contactList = result.invocationResult.contact; 
          var i = 0;
          for (i = 0; i < contactList.length; i++) {
            var currentContact = contactList[i];
            html = html + '<li><a href="javascript:showContactDetail('
                + currentContact.contactId + ')">'
                + currentContact.firstName + ' '
                + currentContact.lastName + '</a></li>';
          }
        }
        jq("#displayContact").html(html);
        jq("#displayContact").listview('refresh');
        busyIndicator.hide();
      } catch (e) {
        busyIndicator.hide();
        displayError(e.toString());
      }
    }
    
    The highlighted code displays how i am getting the actual contact list from the response. This is how the response of the REST service looks like when i hit it directly
    This is how the response that WorkLight adapter is returning to the JavaScript client looks like

2 comments:

Kashif Amin Makhdoom (MAK) said...

I am getting the error "Object[object Object]" has no method 'listview' ". Please help

Kashif Amin Makhdoom (MAK) said...

When trying your example i am getting the error "Object[object Object] has no method 'listview'". Please help