cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Calling a TWX service from javascript

grtaylor
6-Contributor

Calling a TWX service from javascript

Hi all,

Challenge:

I need to send some info back to the Thingworx server from a Hololens experience.

Issue:

Binding an 'APPLICATION EVENT' to the service on a thing in the 'EXTERNAL DATA' (all done in the right hand pane) is limiting and although works in preview and in 2D expereiences, it is not working for me in Hololens experiences.

I thought I would use a programmatic way to call the TWX service (as seen in others posts):

i.e.

var result = $scope.app.mdl['IIM.Workflow.REST'].svc['ConsumeAnswerToQuestion'] ; 


This doesn't work in hololens - even if I've set the service to Invoke on startup - which appears to be needed before the service can be sucessfully referenced in js.

Workaround:

For now, I've found another way of calling a thingworx service from JS - which  appears to be robust, and can asyncronously deal with response when it arrives back :-

  $http({

   method: 'POST',

    url: '/Thingworx/Things/IIM.Workflow.REST/Services/ConsumeAnswerToQuestion',

    headers: {

   'Content-Type': 'application/json',

    'Accept': 'application/json'

   },

   data: {

    "Answer": $scope.app.params.answer

   }

   })

  .success(function(data, status, headers, config){

    console.log("status = " + status);

    console.log("data = %O", data);

   })

  .error(function(data, status, headers, config){

   console.log("status = " + status);

   console.log("data = " + data);

  });

I'm sure there should be an easier way to call a service?  Anyone else experiencing issues calling services from hololens?

Thanks

Graham

2 REPLIES 2
katte
1-Newbie
(To:grtaylor)

Graham Taylor​ As ThingWorx Service (ConsumeAnswerToQuestion) ​  returns result in specific datatype, we need to add "svc['...'].data" to get the result stored in a variable. If the service returns 'infotable', we need to add data[0].<name-of-the-column>

grtaylor
6-Contributor
(To:katte)

Thanks Giri,

I understand that, but the issue I'm having is that referencing this

$scope.app.mdl['IIM.Workflow.REST'].svc['ConsumeAnswerToQuestion']

does not fire the service call at all.

If I run the service by binding it to a button or another event, the service is called, then  I can get the .data out.

It's the firing of the service that is not being triggered programmatically...

Any ideas?

Graham

Top Tags