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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:48:52+00:00 2026-05-25T20:48:52+00:00

I need to retrieve data via cross-domain XMLHttpRequest. To make this work in (almost)

  • 0

I need to retrieve data via cross-domain XMLHttpRequest. To make this work in (almost) all browsers, I use native XHR first and, if that fails, flXHR.

The (working) code I currently have for this is as follows:

jQuery.support.cors = true; // must set this for IE to work

$.ajax({
    url: 'http://site.com/dataToGet', 
    transport : 'xhr',
    success: function(data) {
        console.log('Got data via XHR');
        doStuff(data);
    },
    error: function(xhr, textStatus, error) {
        console.log('Error in xhr:', error.message);
        console.log('Trying flXHR...');
        $.ajax({
            url: 'http://site.com/dataToGet',
            transport : 'flXHRproxy',
            success: function (data) {
                console.log('Got data via flXHR');
                doStuff(data);
            },
            error: function (xhr, textStatus, error) {
                console.log('Error in flXHR:', error.message);
                console.log('Both methods failed, data not retrieved.');
            }
        });
    }
});

This feels like a lot of code duplication to me, especially in the success handlers. Is there a more efficient way to do this? I’d really prefer to make one $.ajax call that would try both transports in turn, instead of having to use the error handler to make the call a second time. It’s not too bad in this example, but rapidly gets more complicated if the success handler is longer or if the success handler has to itself issue another $.ajax 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-05-25T20:48:53+00:00Added an answer on May 25, 2026 at 8:48 pm

    Why don’t you just wrap this in a function by itself? That’s after all, how you end up reusing code. You can even pass functions as arguments to make sure that you don’t have to repeat this code more than once.

    To me this is pretty straight forward but maybe I’ve misunderstood.

    function xhr(success) {
        $.ajax({ 
            success: success, 
            error: function() { 
                $.ajax({ success: success }) 
            } 
        });
    }
    

    Then just pass the success handler once

    xhr(function(data){/*magic*/});
    

    Or if you wanna basically avoid redundant configuration of the ajax call use the first object as a template, like this:

    function xhr(success) {
        var ajaxParams = { success: success };
        ajaxParams.error = function() { 
            $.ajax($.extend(ajaxParams, { transport: 'xhr' })); 
        }
        $.ajax(ajaxParams);
    }
    

    I simplified the whole thing a bit, but I hope you get the point.

    Edit

    Reading that last bit, maybe this will give you some ideas… it’s a variation of that last snippet.

    function xhr(success) {
        var ajaxParams = { success: success };
        ajaxParams.error = function() { 
            var newParams = $.extend(ajaxParams, { transport: 'xhr' });
            newParams.success = function() {
                // do something
                // arguments is a special array, even if no parameters were
                // defined in any arguments where passed they will be found
                // in the order they were passed in the arguments array
                // this makes it possible to forward the call to another 
                // function
                success.apply(this, arguments); 
            }
            $.ajax(newParams); 
        }
        $.ajax(ajaxParams);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I need to retrieve data from database via an ajax call. I am
I need to retrieve data from two tables. the first is a list of
I need to retrieve a set of Widgets from my data access layer, grouped
using jquery I need to retrieve an array from table cells, format the data
I need to retrieve all rows from a table where 2 columns combined are
I'm building a system that need to collect some user sensitive data via secured
I'm working with SharePoint and ProjectServer 2007 via PSI. I need to retrieve the
For example I need to retrieve a value from this session. How should I
I have MySQL database server I need to update and retrieve data to/from MySQL
I need to retrieve change-data-capture rows for several tables, and I'm required (by company

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.