how to return value from ajax function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Trisha8
    New Member
    • Feb 2013
    • 7

    how to return value from ajax function?

    i want to return value from ajax function to the function where ajax call has been made.
    Code:
    function val()
    {
    var temp=ajaxfun();//ajax function;
    }
    
    function ajaxfun()
    {
    //url,type, data will go here and then
    success: function(somedata){
    //here i want to return data i.e. somedata
    //something like "return somedata" to val();
    }
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • Sherin
      New Member
      • Jan 2020
      • 77

      #3
      Try This code

      Code:
      function cityconfirm()
      {
      var cityid=document.getElementById('city').value;
      alert(getcityvalue(cityid));
      return confirm('Are you sure you want to delete')
      }
      
      function getcityvalue(cityid)
      
      {
      
      jQuery.ajax({
      url: 'http://mysite.com/lookupcity.asp?cityid=' + cityid,
      type: 'get',
      dataType: 'text/html',
      success:function(data)
      {
      return(data);
      }
      });
      }

      Comment

      • priti kumari
        New Member
        • Jul 2021
        • 3

        #4
        function follow() {
        get Products(functi on(d) {[URL=["http://pritikumari.000 webhostapp.com/priti[/URL]
        //processing the data
        console.log(d);
        });
        }
        function get Products(callba ck) {
        var data;
        $.ajax({
        u r l: 'u r l',
        data: 'all product data to send',
        success: function (resp) {
        data = resp;
        callback(data)
        },
        error: function () {}
        }); // ajax asyn chronus request
        //the following line wouldn't work, since the function returns immediately
        //return data; // return data from the ajax request
        }

        Comment

        Working...