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 6813363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:33:41+00:00 2026-05-26T20:33:41+00:00

(1. Do you find it useful to have a global jQuery ajax ‘helper’ that

  • 0

(1. Do you find it useful to have a global jQuery ajax ‘helper’ that you can call ajax(url,data,async) ? )

function ajax(ajax_url, ajax_data, ajax_async){
  ajax_async = typeof(ajax_async) != 'undefined' ? ajax_async : true;

  $.ajax({
    type: 'GET',
    url: ajax_url,
    data: ajax_data,
    async: ajax_async,
    dataType: 'text',
    success: function(response) {
        return response;
    },
    error: function(){

    }
  });
}

If I’m calling a function that places a GET and returns the success response.

alert(ajax('localhost/hello','',false);

If I do alert(get_ajax()) I get an alert of ‘undefined’.. even if there was a return value

I see its an event loop issue, but I async:false in the ajax call. thoughts?

  • 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-26T20:33:41+00:00Added an answer on May 26, 2026 at 8:33 pm

    This is one of those questions that has been asked a million times, but it’s nearly impossible to search. Anyway, the problem is that when you return from here…

    success: function(response) {
        return response;
    }
    

    …you’re not returning from the ajax() function but the anonymous success function. To return the value from the main function you have to save the string to a variable so that you can return from the correct function.

    var val = '';
    
    $.ajax({
        //       ....
        success: function(response) {
           val = response;
        },
        //       ....
    });
    
    return val;
    

    Note that this only works when you’re making a synchronous call. For asynchronous calls it’s better to give a callback function as an argument and use it as the success function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a win32 API, developed in c++ and i can't find any useful
I find jsfiddle very useful for client side web development. I have had a
We'd find it very useful to have an AddListener to complement RemovalListener in Google
I have a CI application that uses .htaccess for URL routing. My basic setup
In my useful hotkeys program, i have a global hotkey which sets your current
Why does hibernate do a select before saving an object? I can't find useful
I did the research and couldnt find anything useful I have a problem with
Does anyone have an IsDirty implementation they find particularly useful? Ideally I'd like to
We have been working with CVS for years, and frequently find it useful to
I have learned that in iOS 5, properties that are marked with UI_APPEARANCE_SELECTOR can

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.