Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 882543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:26:34+00:00 2026-05-15T12:26:34+00:00

I have a function runAjax that functions correctly. Unfortunately I am struggling to return

  • 0

I have a function runAjax that functions correctly. Unfortunately I am struggling to return the value I get from the ajax query.

The ajax function assigns the returned value inside “contents” or “error” xml tags to the variable “result”.

If I alert the result variable inside the ajax function it alerts the correct value (i.e if the xml value inside contents is “published” it alerts published).

However if I alert the returned value from the runAjax function it alerts an object instead of the value of the internal variable “result” which in the above example is “published”.

function runAjax (data_obj){
  return $.ajax({
      url:"/ajax.php",
      dataType: "xml",
      data: data_obj,
      success: function(data) {
        // format result
        var xml;
        if (typeof data == "string") {
          xml = new ActiveXObject("Microsoft.XMLDOM");
          xml.async = false;
          xml.loadXML(data);
        } else {
          xml = data;
        }
        var result;
        if($("error",xml).text()){
          result = [$("error",xml).text()];
        } else{
          result = [
            $("contents", xml).text()
          ];
        }
      alert(result); //alerts the correct string for example "published"
      return result;
      }
    });
  }
  $('ul.content li span.changeable').click(function(e){
    e.preventDefault();
    var method_set = $(this).parent().attr("class");
    var id_set = $(this).parent().parent().find('li.id span').html();
    var user = $(this);
    var result = runAjax({method: method_set, id: id_set});
    alert(result); //alerts an object not published

  });

Im sure it has something to do with the way I am returning the variable but I can’t figure it out. Any input would be much appreciated.

Regards
Luke

UPDATE:
This is the revised code that works thanks to all the input from people below:

  function runAjax (data_obj,callback){
    $.ajax({
      url:"/ajax.php",
      dataType: "xml",
      data: data_obj,
      success: function(data) {
        // format result
        var xml;
        if (typeof data == "string") {
          xml = new ActiveXObject("Microsoft.XMLDOM");
          xml.async = false;
          xml.loadXML(data);
        } else {
          xml = data;
        }
        var result;
        if($("error",xml).text()){
          result = [$("error",xml).text()];
        } else{
          result = [
          $("contents", xml).text()
          ];
        }
        if ( typeof(callback) == "function") {
          callback(result);
        }
      }
    });
  }
  $('ul.content li span.changeable').click(function(e){
    e.preventDefault();
    var method_set = $(this).parent().attr("class");
    var id_set = $(this).parent().parent().find('li.id span').html();
    var user = $(this);
    runAjax({
      method: method_set, 
      id: id_set
    },
    function(result){
      $(user).html(result.join('')); //this is instead of alert(result);
    }
    );

  });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:26:35+00:00Added an answer on May 15, 2026 at 12:26 pm

    According to the docs

    The $.ajax() function returns the XMLHttpRequest object that it creates.

    Any return value that you return from the success callback function is ignored.

    You need to put the value in a variable defined in a wider scope than inside the callback function (global, or preferably inside an outer function).

       var result;
       $.ajax({
           ....
           success : function(data) {
              ...
              result = ...;
           }
       });
    

    Or better yet: do whatever you want to do with the return value inside the success callback function, this will keep the asynchronous nature of the ajax call and means you don’t need to wait for the call to come back.

    Doing your processing in the success callback function means you know you have the results, if you put the value in a variable the variable may not be assigned a value yet by the time you want to use it.

    In a comment to another answer on this page you say:

    however I am calling the runAjax function from multiple other functions not just the one in my code example above, so I need the value returned rather than the runAjax function doing the html replacing

    I would add an extra parameter to your runAjax function, which is another callback function that you can pass in different processing functions from the various functions.

    function runAjax(data_obj, callback) {
        $.ajax({
            ...
            success : function(data) { 
                ...
                result = ...
                ...
                if ( typeof(callback) == "function") {
                    callback(result);
                }
            }
        });
    }
    

    Then you can call it like

    runAjax({method: method_set, id: id_set},
        function(result){
             alert(result);
        }
    );
    

    Then you can do your generic processing of the data in the success function, but the custom processing for each call in the callback function.

    If you really need to wait for the call, you can create a synchronous ajax call by passing the async option:

     $.ajax({
        async:false,
        ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that gets x(a value) and xs(a list) and removes all
I have a function, parseQuery, that parses a SQL query into an abstract representation
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that I use called sqlf(), it emulates prepared statements. For
I have a function that is effectively a replacement for print, and I want
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have a function where I need to do something to a string. I
I have a function in a native DLL defined as follows: #include <string> void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.