468,301 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

how to return value from ajax function?

i want to return value from ajax function to the function where ajax call has been made.
Expand|Select|Wrap|Line Numbers
  1. function val()
  2. {
  3. var temp=ajaxfun();//ajax function;
  4. }
  5.  
  6. function ajaxfun()
  7. {
  8. //url,type, data will go here and then
  9. success: function(somedata){
  10. //here i want to return data i.e. somedata
  11. //something like "return somedata" to val();
  12. }
  13. }
  14.  
Mar 29 '13 #1
2 2578
Dormilich
8,651 Expert Mod 8TB
it doesn’t work like that with asynchronous AJAX calls.

you could of course use a synchronous call, but the you wouldn’t need a success callback function either.
Mar 29 '13 #2
Sherin
77 64KB
Try This code

Expand|Select|Wrap|Line Numbers
  1. function cityconfirm()
  2. {
  3. var cityid=document.getElementById('city').value;
  4. alert(getcityvalue(cityid));
  5. return confirm('Are you sure you want to delete')
  6. }
  7.  
  8. function getcityvalue(cityid)
  9.  
  10. {
  11.  
  12. jQuery.ajax({
  13. url: 'http://mysite.com/lookupcity.asp?cityid=' + cityid,
  14. type: 'get',
  15. dataType: 'text/html',
  16. success:function(data)
  17. {
  18. return(data);
  19. }
  20. });
  21. }
Feb 22 '21 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Michael Satterwhite | last post: by
5 posts views Thread by Bob | last post: by
1 post views Thread by Seong-Kook Shin | last post: by
3 posts views Thread by b_naick | last post: by
11 posts views Thread by igor.lautar | last post: by
4 posts views Thread by shapper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.