tags:

views:

36

answers:

2

Hi, I am using the below method between the 50lines of JQuery Code. From the Json Result, I have to do the Further process. What it does is, When first time it is not calling the specified url and debuuger not hit inside to the JSon. After all the below code runs then, It fetches the result and debugger(inside Json Method) is also hit. Could you explain me, In which way it is working. whether it is because of AJAX Call it is allowing next code to run before completing it.

var specExists = false;
url = '/CompanyAdd/SpecMathesWithProvAndContract'; debugger
$.getJSON(url, { OffId: OffID, CntId: CntID }, function(data) {
     debugger
     if (data) { debugger
     specExists = true;   
}   });

Thanks, Nizam

A: 

The function(data) will only be called after the result was returned from the server. This is because that function is a callback function, meaning it will only execute after a JSON request is completed.

So, in other words, when the Result is obtained, then Function(data) will be called.

Ngu Soon Hui
Hi, Then Can I assume that it allows to execute the next line before get the Json Result. When get the Result then Function(data) will call. Am I right?Thanks,Nizam
nizam
Hi, Is there any other way get result from the Json and the Allow the next code to execute.Thanks,Nizam
nizam
Thank You very much for clarifying me...
nizam
*When get the Result then Function(data) will call. Am I right?*-- Yes
Ngu Soon Hui
A: 

Ngu Soon Hui's answer is correct.

after executing ajax function, it will execute next code. the callback function will be executed asynchronously, like running another thread, when ajax is completed.

but we can make it wait until ajax call completed before executing next code by specify option async = false in $.ajax() function. $.getJSON() doesn't have this option.

$.ajax({
  type: "POST",
  url: "/CompanyAdd/SpecMathesWithProvAndContract",
  dataType: "json",
  data: { OffId: OffID, CntId: CntID },
  async: false
});
Anwar Chandra
Thank You Very much for clarifying me..
nizam