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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:37:31+00:00 2026-05-11T14:37:31+00:00

I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing

  • 0

I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing in my page. Each code block has three functions (code is incomplete and for illustrative purpose only):

  1. encapsulates $.ajax call:

    function doSomething(data){    // do some preprocessing     $.ajax({}); // some JQuery Ajax operation that accepts data    // do some postprocessing   return false; }
  2. handles the response:

    function handleResponse(result){     // process the result   return false; }
  3. handles any error:

    function handleError(XMLHttpRequest, textStatus, errorThrown){     // gracefully handle the Error and present relevant information to user   return false; }

In a page that requires a lot of data processing I end up having a lot of these blocks which seems to be duplication, so I decided to do some refactoring.

I figure there would be different ways to go about this.

  1. One could have one error handler that can be reused across Ajax calls (obvious).
  2. One could maybe reuse some response handlers, but this would be akward as responses are very different depending on call.
  3. Maybe create some kind of prototype object that provides base functionality and have a static method for error handling (can this be done in JavaScript?).

I was just wondering if anyone has come across this and/or if there is a best practice solution for this?

  • 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. 2026-05-11T14:37:32+00:00Added an answer on May 11, 2026 at 2:37 pm

    You can use the $.ajaxSetup({}) method in jQuery to setup some common ajax settings.

    For instance, if you are going to be posting to the same URL over and over again on some page, you can just set that in the ajaxSetup. This would mean you would have to pass less parameters to a function like what Richard provided. Any property of the ajax method’s first parameter can be set as a default in $.ajaxSetup().

    $.ajaxSetup({     url: 'my/ajax/url'     success: function() {         // Do some default stuff on success     },     error: function() {         // Do some default stuff on a failure     }     // etc... }); 

    They can be overridden in any ajax call. So, now you can just do:

    $.ajax({data:{foo:'bar',bar:'foo'}}); 

    And you can override the URL, for instance like this:

    $.ajax({url:'different/ajax/url',data:{foo:'bar',bar:'foo'}}); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Surely you just need to check the current zoomScale as… May 12, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer I'm afraid you'll have to rename your Model model to… May 12, 2026 at 3:13 pm
  • Editorial Team
    Editorial Team added an answer class Team: def __init__(self): self.name = None self.logo = None… May 12, 2026 at 3:13 pm

Related Questions

I have two divs inside of a container. One on the left, one on
I am trying to understand the internals of how jquery framework is written and
I am currently working on a web app using Wicket and started using jQuery
This should be easy (at least no one else seems to be having a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.