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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:24:35+00:00 2026-05-23T07:24:35+00:00

I am trying to create a JavaScript object that will deal with all of

  • 0

I am trying to create a JavaScript object that will deal with all of my ajax calls instead of making several different ajax handlers have one that does the job for all.

So here’s what I have so far

My PHP files that are going to be called within my AJAX Handler are placed in a directory called ajax, the directories within that are named to relate to the pages e.g. the jobs page has its own directory and so on. within these directories I have placed the page relevant PHP files.

so now to my ajax Handler object code:

function ajaxHandler(pageName,functionCall){
this.pageName       = pageName;
this.functionCall   = functionCall;

// set functions
this.getPage = getPage;
this.setPage = setPage;
this.getFunctionCall = getFunctionCall;
this.setFunctionCall = setFunctionCall;
this.performAjaxCall = performAjaxCall;
}
// accessor for current page
function getPage(){
    return this.pageName;
}

// accessor for setting the current page
function setPage(page){
    this.pageName = page;
}

// accessor for retrieving the current functionCall
function getFunctionCall(){
    return this.functionCall;
}

// accessor for setting the current function call
function setFunctionCall(func){
    this.functionCall = func;
}

// perform the loaded ajax call
// DATA : must be in the form of JSON
function performAjaxCall(data){
    $.ajax({
       type     : 'POST',
       url      : '/ajax/'+ this.pageName + '/' + this.functionCall + '.php',
       dataType : 'json',
       data     : data,
       success  : function(data){
          return data;
       },
       error    : function(xhr, ajaxOptions, thrownError){
          return {"error":true,"msg":thrownError};
       }
    })
 }

so with this file called and working fine, apart from when performAjaxCall is called the ajax works perfectly but the data being returned is being seen as undefined I will show you an example

function getActiveJobs(initPage){
   var ajh = new ajaxHandler('jobs','getActiveJobs');
   var req = {'page' : 1};
   var data = ajh.performAjaxCall(req);
   alert(data.error);
}

The alert returns undefined, I suspect it is because the JavaScript is not waiting for the data to be returned and as such data is not yet defined, but I am unsure I thought I would ask here before leading myself down a blind alley.

  • 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-23T07:24:36+00:00Added an answer on May 23, 2026 at 7:24 am

    You are right, JavaScript is not waiting. performAjaxCall returns before the response of request is received. But even it were waiting, you cannot return a value from a callback like this:

    success  : function(data){
          return data;
    },
    

    You are passing this function to $.ajax, so it returns ne value only inside the $.ajax function. It does not have any effect on the outer performAjaxCall function.

    You have to pass another function which will process the data, something like:

    function performAjaxCall(data, cb){
        $.ajax({
           //...
           success: cb,
           error: cb
        });
     }
    

    and

    function getActiveJobs(initPage){
       var ajh = new ajaxHandler('jobs','getActiveJobs');
       var req = {'page' : 1};
       ajh.performAjaxCall(req, function(data) {
           alert(data.error);
       });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.