Bug Tracker

Opened 7 years ago

Closed 6 years ago

#13862 closed feature (worksforme)

Add possibility of passing arguments to XMLHttpRequest constructor

Reported by: Veneti Owned by: jaubourg
Priority: high Milestone: 1.next/2.next
Component: ajax Version: 1.9.1
Keywords: Cc:
Blocked by: Blocking:

Description

Making AJAX requests on Firefox OS (operating system, not regular browser) does not work unless XMLHttpRequest is created in the following way:

var xhr = new window.XMLHttpRequest({mozSystem:true});

What makes difference is 'mozSystem'. In another ticket #13394 someone proposed setting it via xhrFields however mozSystem is read-only and can not be set once XMLHttpRequest object is created. It can be set only on creation:

https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest

Change History (5)

comment:1 Changed 7 years ago by jaubourg

Component: unfiledajax
Milestone: None1.10/2.0
Owner: set to jaubourg
Priority: undecidedhigh
Status: newassigned
Type: bugfeature

You can already handle the problem 3 different ways:

  1. global override, no condition:
    $.ajaxSetup( {
      xhr: function() {
        return new window.XMLHttpRequest( {
          mozSystem: true
        } );
      }
    } );
    
  2. local override, simple condition:
    $.ajaxPrefilter( function( options ) {
      if ( options.firefoxOS ) {
        options.xhr = function() {
          return new window.XMLHttpRequest( {
            mozSystem: true
          } );
        }
      }
    } );
    
    // Then later on
    
    $.ajaxSetup( {
      firefoxOS: true
    } );
    
  3. Generic constructor parameter
    $.ajaxPrefilter( function( options ) {
      if ( options.xhrConstructParam ) {
        options.xhr = function() {
          return new window.XMLHttpRequest( options.xhrConstructParam );
        }
      }
    } );
    
    // Then later on
    
    $.ajaxSetup( {
      xhrConstructParam: {
        mozSystem: true
      }
    } );
    
  1. is simpler but cannot be used generically in a project that may run outside of FirefoxOS,
  2. is better at isolating FirefoxOS specific code,
  3. is to be used for a more generic approach.

Anyway, we should probably consider adding a standard option (as in 3) seeing as the options object param is part of the standard now: http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest

comment:2 Changed 7 years ago by jaubourg

Milestone: 1.10/2.01.11/2.1

comment:3 Changed 6 years ago by dmethvin

Milestone: 1.11/2.11.next/2.next

comment:4 Changed 6 years ago by dmethvin

Method 1 seems good enough here? If we're going to add more code we need docs before landing it, and I shudder to think of more docs for ajax.

comment:5 Changed 6 years ago by dmethvin

Resolution: worksforme
Status: assignedclosed

Several good solutions were given above so it seems like we can avoid another mechanism and the docs it would require.

Note: See TracTickets for help on using tickets.