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

  • Home
  • SEARCH
  • 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 8472007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:57:00+00:00 2026-06-10T16:57:00+00:00

Possible Duplicate: Return Value from inside of $.ajax() function I’m working on a CakePHP

  • 0

Possible Duplicate:
Return Value from inside of $.ajax() function

I’m working on a CakePHP app that makes use of widespread AJAX calls to controllers. I’m stuck with one particular AJAX call in which I’m trying to assign the response from the controller to a JS global variable. Here is the code:

window.errors = "";

function setErrors(err) {
    window.errors = err;
}

function ajaxCall(u, t, d, dt) {
    var type = typeof t !== 'undefined' ? t : "post";
    var dataType = typeof dt !== 'undefined' ? dt : "json";
    var success = false;
    var err = "";
    $.ajax({url: url, data: "data=" + d, type: type, dataType: dataType,
        success: function(d){
                if(d.hasOwnProperty('success') === false) { //json response
                    for(var i in d) { //fetch validation errors from object
                        for(var j in i) {
                            if(typeof d[i][j] === "undefined") {
                                continue;
                            }
                            err += d[i][j] + "<br/>";
                        }
                    }
                    console.log(err); //<=== displays correct value
                    setErrors(err); //<=== but somehow this seems to be failing??
                }
                else {
                   if(d.success === "1") {
                       success = true;
                }
            }
        }
    });
    return success; //<=== I suspect this might be the culprit
}

And this is how ajaxCall() is used:

function register() {
    var data = {};
    var $inputs = $("#regForm :input");
    $inputs.each(function(){
        data[this.name] = $(this).val();
    });
    data = {"User" : data }; //CakePHP compatible object
    data = JSON.stringify(data);

    //Here's the AJAX call
    if(ajaxCall('https://localhost/users/add', 'post', data, 'json')) {
        alert("Registered!");
    }
    else {
        alert(window.errors); // <=== empty string
        console.log("window.errors is: " + window.errors); // <=== empty string
    }
}

But on the Chrome JS console, window.errors returns the correct value (non-empty, validation error string).

I found a similar question that possibly might be addressing my issue (the return success immediately following the $.ajax() is being executed before the success: callback). How can I fix this without drastically changing the code (also, I don’t want to make this a synchronous call)?

  • 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-06-10T16:57:02+00:00Added an answer on June 10, 2026 at 4:57 pm

    Yes, you are right that the return statement runs before the success callback. You can’t return the result from the function, as the function has to return before the success event can be handled.

    Add a callback to the ajaxCall function, and call that instead of setting the success variable:

    function ajaxCall(u, t, d, dt, callback) {
      var type = typeof t !== 'undefined' ? t : "post";
      var dataType = typeof dt !== 'undefined' ? dt : "json";
      $.ajax({url: url, data: "data=" + d, type: type, dataType: dataType,
        success: function(d){
          if(d.hasOwnProperty('success') === false) { //json response
            for(var i in d) { //fetch validation errors from object
              for(var j in i) {
                if(typeof d[i][j] === "undefined") {
                  continue;
                }
                err += d[i][j] + "<br/>";
              }
            }
            callback(false, err);
          } else {
            callback(d.success === "1", "");
          }
        }
      });
    }
    

    Send the code for handling the result into the ajaxCall function:

    ajaxCall('https://localhost/users/add', 'post', data, 'json', function(success, err){
      if (success) {
        alert("Registered!");
      } else {
        alert(err);
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) {
Possible Duplicate: Why can you return from a non-void function without returning a value
Possible Duplicate: Why can you return from a non-void function without returning a value
Possible Duplicate: Jquery: Return the value of a ajax call to caller function? In
Possible Duplicate: Return value from thread I want to get the free memory of
Possible Duplicate: Return value from local scope? #include <stdio.h> int main() { int x
Possible Duplicate: Is a return value of 0 from write(2) in C an error?
Possible Duplicate: What does Asynchronous means in Ajax? jQuery ajax return value Trying to
Possible Duplicate: jQuery: Return data after ajax call success jQuery AJAX: return value on
Possible Duplicate: return AJAX callback return I have made a JQuery code using AJAX,

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.