How does ASP.NET AJAX work when getting values from the server?
If I want to call a server function from javascript to get the person's name from the database (as an example) ... and I went ...
name = myServices.getName (userId);
If I have a manager script with a service reference to an .asmx file that has a getName (int userId) {} method, then this function should be called correctly and will eventually return the name for that userId.
Sorry, I want to do ....
name = myServices.getName (userId); warning (name);
however when you do ASP.NET AJAX it will invoke the web method and continue execution before waiting for a response from the server (which I understand is the point of ajax to stop the browser from hovering waiting for data)
I need to get the name back from the server before I can continue executing ... How can I go about this to fix this issue?
There is a method that you can add as a parameter to the service method that will call the success method where you can do other things.
For instance:
function test(){
PageMethods.MyMethod("name", OnMyMethodComplete);
}
function OnMyMethodComplete(result, userContext, methodName)
{
alert(result);
}
a source to share
If you want to call a web method synchronously, you will need to set up the request manually and use Sys.Net.XMLHttpSyncExecutor.
Here's an example (see ExecuteSynchronously function)
a source to share
For a javascript solution, you can loop until the name gets a value. Adjust latency based time to keep the app live
var time = 100;
window.setTimeout(name = '' ? wait : continue, time);
function wait()
{
window.setTimeout(name = '' ? wait : continue, time);
}
function continue()
{
//code having to do with name
alert(name)
}
a source to share