Skip to main content

Bug Tracker

Side navigation

#1118 closed feature (duplicate)

Opened April 10, 2007 08:02PM UTC

Closed November 01, 2010 09:28PM UTC

Last modified March 15, 2012 01:24AM UTC

Migrate AJAX setRequestHeader() to a mapping in ajaxSettings

Reported by: DanSwitzer2 Owned by:
Priority: major Milestone: 1.1.3
Component: ajax Version: 1.1.2
Keywords: Cc:
Blocked by: Blocking:
Description

Code could be reduced and flexibility increase by adding a "headers" key to the ajaxSettings defaults, which could be used to specify additional headers to send with each AJAX request.

The following documents what changes need to be made:

Add a "headers" key to the ajaxSettings defaults:

ajaxSettings: {

global: true,

type: "GET",

timeout: 0,

contentType: "application/x-www-form-urlencoded",

processData: true,

async: true,

data: null,

headers: {"X-Requested-With": "XMLHttpRequest"}

},

Remove the following lines:

Set header so the called script knows that it's an XMLHttpRequest

xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");

Make sure the browser sends the right content length

if ( xml.overrideMimeType )

xml.setRequestHeader("Connection", "close");

Add the following line:

// supply any additional mapped headers

for( var k in s ) xml.setRequestHeader(k, s[k]);

That's it! This will make it easier to add headers to a request, reduces the code and removes the old "Connection: close" fix for older Gecko-based browsers (which are no longer supported by jQuery anyway.)

-Dan

Attachments (0)
Change History (3)

Changed June 15, 2010 02:54AM UTC by dmethvin comment:1

description: Code could be reduced and flexibility increase by adding a "headers" key to the ajaxSettings defaults, which could be used to specify additional headers to send with each AJAX request.\ \ The following documents what changes need to be made:\ \ Add a "headers" key to the ajaxSettings defaults:\ \ ajaxSettings: {\ global: true,\ type: "GET",\ timeout: 0,\ contentType: "application/x-www-form-urlencoded",\ processData: true,\ async: true,\ data: null,\ headers: {"X-Requested-With": "XMLHttpRequest"}\ },\ \ Remove the following lines:\ \ // Set header so the called script knows that it's an XMLHttpRequest\ xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");\ \ // Make sure the browser sends the right content length\ if ( xml.overrideMimeType )\ xml.setRequestHeader("Connection", "close");\ \ Add the following line:\ \ // supply any additional mapped headers\ for( var k in s ) xml.setRequestHeader(k, s[k]);\ \ That's it! This will make it easier to add headers to a request, reduces the code and removes the old "Connection: close" fix for older Gecko-based browsers (which are no longer supported by jQuery anyway.)\ \ -DanCode could be reduced and flexibility increase by adding a "headers" key to the ajaxSettings defaults, which could be used to specify additional headers to send with each AJAX request. \ \ The following documents what changes need to be made: \ \ Add a "headers" key to the ajaxSettings defaults: \ \ ajaxSettings: { \ global: true, \ type: "GET", \ timeout: 0, \ contentType: "application/x-www-form-urlencoded", \ processData: true, \ async: true, \ data: null, \ headers: {"X-Requested-With": "XMLHttpRequest"} \ }, \ \ Remove the following lines: \ \ // Set header so the called script knows that it's an XMLHttpRequest \ xml.setRequestHeader("X-Requested-With", "XMLHttpRequest"); \ \ // Make sure the browser sends the right content length \ if ( xml.overrideMimeType ) \ xml.setRequestHeader("Connection", "close"); \ \ Add the following line: \ \ // supply any additional mapped headers \ for( var k in s ) xml.setRequestHeader(k, s[k]); \ \ That's it! This will make it easier to add headers to a request, reduces the code and removes the old "Connection: close" fix for older Gecko-based browsers (which are no longer supported by jQuery anyway.) \ \ -Dan

See also #4453.

Changed November 01, 2010 09:28PM UTC by rwaldron comment:2

resolution: → duplicate
status: newclosed

Changed November 01, 2010 09:28PM UTC by rwaldron comment:3

Duplicate of #4453.