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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:11:07+00:00 2026-05-20T08:11:07+00:00

I need a little help in my application design. Using Ajax I want to

  • 0

I need a little help in my application design. Using Ajax I want to get some PHP resources consecutively but I don’t think if it is good to retrieve them using JQuery $.ajax method.

I think something like this means wrong design:

$.ajax({
     url: SERVERURL+'index.php/home/checkAvailability',
     datatype: 'text',
     success: function(data){
        if(data == 'unavailable'){
           // do stuff
        }
        else{
           $.ajax({
              url: SERVERURL+'index.php/home/getWebTree/',
              dataType: 'json',
              success: function(data){
                 // do stuff
              }
           });
        }
     }
  });

Can anybody give me a suggestion to get a better design? How can I do the same in a better way?

Thanks!

EDIT: like @arnorhs tell us, using async parameter could be a solution. But I’m still think that there are other solutions instead of using consecutive ajax calls.

EDIT2: checkAvailability and getWebTree are PHP functions using CodeIgniter that I’ve developed to get resources from an external server using Http_Request object.

function checkAvailability() {
      $this->load->library('pearloader');
      $http_request = $this->pearloader->load('HTTP', 'Request');
      $http_request->setURL('http://myurl');
      $http_request->_timeout = 5;
      $http_request->sendRequest();
      $res = 'available';
      if (!$http_request->getResponseCode())
         $res = 'unavailable';
      return $res;
   }
  • 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-20T08:11:07+00:00Added an answer on May 20, 2026 at 8:11 am

    Doing the calls all the same time

    If you want to do all the ajax calls at the same time, you can simply call an ajax request right after the others. You could even assign them the same success handler. If you want a more "elegant" approach, I would do something like this:

    // define a set of requests to perform - you could also provide each one
    // with their own event handlers..
    var requests = [
        { url: 'http://someurl', data: yourParams   },
        { url: 'http://someurl', data: yourParams   },
        { url: 'http://someurl', data: yourParams   },
        { url: 'http://someurl', data: yourParams   }
    ];
    
    var successHandler = function (data) {
        // do something
    }
    
    // these will basically all execute at the same time:
    for (var i = 0, l = requests.length; i < l; i++) {
        $.ajax({
            url: requests[i].url,
            data: requests[i].data,
            dataType: 'text',
            success: successHandler
        });
    }
    

    .

    Do a single request

    I don’t know your use case, but of course what you really should be trying to do is retrieve all the data you’re retrieving in a single request. That won’t put a strain on your server, the site/application will seem faster to the user and is a better long term approach.

    I would try to combine checkAvailability and getWebTree into a single request. Instead of receiving the data in Javascript as text objects, a better approach would be to receive them as json data. Luckily PHP provides very easy functions to convert objects and arrays to json, so you’ll be able to work with those objects pretty easily.

    edit: small modifications in the PHP code now that I understand your use case better.

    So something like this in the PHP/CI code:

    function getRequestData () {
        if (checkAvailability() == 'available') {
            $retval = array (
                'available' => '1',
                'getWebTree' => getWebTree()
            );
        } else {
            $retval = array (
                'available' => '0'
            );
        }   
        header('Content-type: text/javascript; charset=UTF-8');     
        echo json_encode($retval););
    }
    

    And the Javascript code can then access those by a single ajax request:

    $.ajax({
        url: 'http://yoururl/getRequestData',
        dataType: 'json',
        success: function (jsonData) {
            // we can now access the parameters like this:
            if (jsonData.checkAvailability) {
                // etc
            }
            //and of course do something with the web tree:
            json.getWebTree
        }
    });
    

    .

    Execute the requests synchronously

    If you set the async parameter in the $.ajax options to false the functions will be made in a synchronous fashion so your code halts until execution has been completed.. or as the documentation says:

    asyncBoolean

    Default: true

    By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

    See http://api.jquery.com/jQuery.ajax/

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

Sidebar

Related Questions

We need some input on what is a good design pattern on using AJAX
Need a little help with my jquery here I want all my button with
I need a little help here: I get a file from an HTML upload
I need a little help with my .htaccess before I deploy it! I want
Hey, I'm creating a little GLUT application and need help with hiding/removing the console
I need a little information/help/suggestions how can I build my application so I can
I need a little help on this subject. I have a Web application written
I need a little help with something that I want to achieve. A want
I'm not a PHP coder so I need a little help with PHP AES
I have little knowledge of java scripting, and need help making my application work.

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.