Problem invoking service from Javascript

This forum contains topics that were moved from Get Satisfaction
Alan Smith
Posts: 0
Joined: Tue Jun 10, 2014 4:59 pm

Problem invoking service from Javascript

Post 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?

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Problem invoking service from Javascript

Post 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

Alan Smith
Posts: 0
Joined: Tue Jun 10, 2014 4:59 pm

Problem invoking service from Javascript

Post 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);

},

??

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Problem invoking service from Javascript

Post 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.

Alan Smith
Posts: 0
Joined: Tue Jun 10, 2014 4:59 pm

Problem invoking service from Javascript

Post 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?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Problem invoking service from Javascript

Post 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/...

Post Reply