Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
my javascript code gets some info from the server

javacode:
CallGenerateIDHandler();

function CallGenerateIDHandler() {
    $.ajax({
        url: "GenerateID.ashx",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: { 'Type': 'ID' },
        responseType: "json",
        success: OnComplete,
        error: OnFail
    });
    return false;
}
function OnComplete(result) {
    alert([result.Id]);
}
function OnFail(result) {
    alert('Request failed');

}


the
alert([result.Id]);
alert me the result I want but how can I return this value instead of
return false;
Posted
Updated 28-Aug-15 3:31am
v3
Comments
Sinisa Hajnal 28-Aug-15 6:19am    
Google MVC (or .NET) json result.
Amit Jadli 28-Aug-15 7:58am    
http://www.codeproject.com/Articles/203621/Call-HTTPhandler-from-jQuery-Pass-data-and-retriev
Lalyka 28-Aug-15 8:32am    
Thanks the link is so helpfull. Just I want to return sth from handler, I do not want to pass any data from ajax.
can you guide me how to set the jason?
F-ES Sitecore 28-Aug-15 9:16am    
I don't understand....if the alert works then you already have the value you want at the client, so what's the issue?
Lalyka 28-Aug-15 9:24am    
I want to return the value, instead of return false

1 solution

Ok now I see it :).

Your problem is that you are making ajax call which is asynchronous operation, that is why there is a callback(OnComplete/OnFail) function that is called after you get your response/error back.

So you can't return the values from CallGenerateIDHandler(); because this function runs synchronously.

What you can do is save value to to some element/localstorage in you OnComplete function. And then call process it.

What exactly are you trying to do?

Another solution is make synchronous call. same thing you have now but add
async:false,

Note:
This is going to work for you, but the problem with this approach is that it will block you UI thread so page will not respond until you get response back. That is very important thing to mention!
 
Share this answer
 
v3
Comments
Lalyka 28-Aug-15 9:04am    
I update my question
xszaboj 28-Aug-15 9:31am    
ok and what is the problem now?
Lalyka 28-Aug-15 9:33am    
sorry for this question because I am so new in javascript.
instead of doing return false; I want to return the value that is alerting
Lalyka 28-Aug-15 9:34am    
in fact I want CallGenerateIDHandler() return my result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900