Page 1 of 1

Problem invoking service from Javascript

Posted: Tue Oct 21, 2014 6:39 pm
by Alan Smith

I am having problems invoking a REST service through Javascript.

I've read through the forum and tried everything I can find but I still cant get it to work.

My rest service is called: checkJobStatus

And on page load I have a function call:

checkJobStatusFunction();

And the function, which isnt quite finished but the basics are there

function checkJobStatusFunction() {

Code: Select all

 // get user pin from local storage 
 var userPIN = localStorage.getItem('current_job_id'); 

 // get current job id from local storage 
 var jobID = localStorage.getItem('user_id'); 

 // invoke checkJobStatus REST service 
 checkJobStatus.execute({ 
     data:{ 
     'checkJobStatusID': jobID 
     }, 
     success: function(e) { 

alert(data); 

     }, 
 }); 

}

I get the following error: Uncaught TypeError: undefined is not a function

Which points me to this line in the function: checkJobStatus.execute({

Can you see what I am doing wrong?


Problem invoking service from Javascript

Posted: Tue Oct 21, 2014 7:46 pm
by Kateryna Grynko

Hi Alan,

Please make sure you are trying to call a service instance, not a service itself, i.e.
Image

Please invoke service instance as follows: prerestservice2.execute(); /pre


Problem invoking service from Javascript

Posted: Tue Oct 21, 2014 8:21 pm
by Alan Smith

Hi Kateryna

That was exactly the problem, thank you.

Just further on this though. If I'm returning a value from the REST service, how do I collect that via the function and insert it into a local variable?

I assume i get it from the below?

success: function(e) {

alert(data);

},

??


Problem invoking service from Javascript

Posted: Wed Oct 22, 2014 3:35 am
by Yurii Orishchuk

Hi Alan,

You can use following code to handle response and store it into LSV:

pre

success: function(e) {

Code: Select all

 //Convert object to JSON string. 
 var dataJSON = JSON.stringify(data); 

 //Store JSON string into LSV. 
 localStorage.setItem("lsnName", dataJSON) 

},

/pre

To get this object for further use in future you can use following code:

pre

Code: Select all

 //Get json string. 
 var dataJSON = localStorage.getItem("lsnName") 

 //Convert JSON.string into JS object. 
 var data = JSON.parse(dataJSON); 

/pre

Regards.


Problem invoking service from Javascript

Posted: Wed Oct 22, 2014 9:47 am
by Alan Smith

Is there a way to invoke the service from several pages. As you can see from above, I am running the function on every page load, so if I use the instance to invoke the service, it will only work on that page.

Is there a way this can be done, to invoke the service without it being page dependent?


Problem invoking service from Javascript

Posted: Wed Oct 22, 2014 11:10 am
by Evgene Karachevtsev

Alan,

If you have render in one html then it is not required. You can also use js to create a global service, please look at these links:
https://getsatisfaction.com/apperyio/...
https://getsatisfaction.com/apperyio/...