Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

I need help, I am executing below service and I have a alert msg inside the success clause of execute service and then I have alert msg outside the execute service. Ideally it should execute the service first and then call the second alert but instead its calling second alert first.

How to synchronize execute and subsequent statements in JS? Please advise. I read various similar question on this topic but couldnt get it working. I tried using deffered object.

What I am trying to achieve is by calling service I am populating variable dynARef and then add this variable value to 'a' tag by using statement $(this).find("div").find("a").html(dynARef) but the value is blank as its executing $(this).find("div").find("a").html(dynARef) and executeservice asynch manner. Please advise?

$.when (myservicename.execute({})
.done(function() {
alert("Refresh Data1");
});

myservicename.execute({"data":
{ "where": whereclausevalue },
"success": function( data ){
alert("Inside the success method of execute service");
dynARef = data[0].value;
}});

//and then I have one more alert msg out side execute serivce

alert("After execute service......");
$(this).find("div").find("a").html(dynARef);

assign some the value

Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

I need help, I am executing below service and I have a alert msg inside the success clause of execute service and then I have alert msg outside the execute service. Ideally it should execute the service first and then call the second alert but instead its calling second alert first.

How to synchronize execute and subsequent statements in JS? Please advise. I read various similar question on this topic but couldnt get it working. I tried using deffered object too but it didnt help...

$.when (myservicename.execute({})
.done(function() {
alert("Refresh Data1");
});

What I am trying to achieve is by calling service I am populating variable dynARef and then add this variable value to 'a' tag by using statement $(this).find("div").find("a").html(dynARef) but the value is blank as its executing $(this).find("div").find("a").html(dynARef) and executeservice asynch manner. Please advise?

myservicename.execute({"data":
{ "where": whereclausevalue },
"success": function( data ){
alert("Inside the success method of execute service");
dynARef = data[0].value;
}});

//and then I have one more alert msg out side execute serivce

alert("After execute service......");
$(this).find("div").find("a").html(dynARef);

assign some the value

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

executing REST service from JS - how to synchronize

Hello! Working on it. I'll update.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

executing REST service from JS - how to synchronize

Invocation of the services is not synchronized. Run your code pre$(this).find("div").find("a").html(dynARef); /pre on servcice success event (where you have first alert and fill in dynARef variable)

Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

It tried executing my code inside success event but its doesn't set the html. Were as same code work perfectly fine outside the success event. Could you please advise why below code is not working inside success event.

$(this).find("div").find("a").html(dynARef);

Thanks for your help Maryna!

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

executing REST service from JS - how to synchronize

Hi Kartik,

Please check the value of 'this' when running the code (F12). Does it contain needed tags?

Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

If I put the below alert inside success event then it say undefine but when I put it outside it gives me required tag properly.

alert(" added new tag"+$(this).find("div").html());

Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

Is this a bug $(this) doesn't work inside success event? Or I am missing something? Please advise?

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

executing REST service from JS - how to synchronize

Kartik,

Output this value in console please: preconsole.log(this);/pre

Kartik A
Posts: 0
Joined: Wed Nov 06, 2013 4:16 pm

executing REST service from JS - how to synchronize

I added below console inside and outside success event as shown below-

console.log("this value is outside success event : "+this);
console.log("this value is inside success event : "+this);

Following is printed on console-

this value is outside success event : [object HTMLDivElement]

this value is inside success event : [object Object]

Return to “Issues”