Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
705216dc
Commit
705216dc
authored
Oct 28, 2013
by
Richard Gibson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
No ticket: Small ajax/xhr size optimizations
parent
f9d41ac6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
6 deletions
+19
-6
xhr.js
src/ajax/xhr.js
+19
-6
No files found.
src/ajax/xhr.js
View file @
705216dc
...
@@ -10,7 +10,8 @@ jQuery.ajaxSettings.xhr = function() {
...
@@ -10,7 +10,8 @@ jQuery.ajaxSettings.xhr = function() {
}
catch
(
e
)
{}
}
catch
(
e
)
{}
};
};
var
xhrSupported
=
jQuery
.
ajaxSettings
.
xhr
(),
var
xhrId
=
0
,
xhrCallbacks
=
{},
xhrSuccessStatus
=
{
xhrSuccessStatus
=
{
// file protocol always yields status code 0, assume 200
// file protocol always yields status code 0, assume 200
0
:
200
,
0
:
200
,
...
@@ -18,8 +19,7 @@ var xhrSupported = jQuery.ajaxSettings.xhr(),
...
@@ -18,8 +19,7 @@ var xhrSupported = jQuery.ajaxSettings.xhr(),
// #1450: sometimes IE returns 1223 when it should be 204
// #1450: sometimes IE returns 1223 when it should be 204
1223
:
204
1223
:
204
},
},
xhrId
=
0
,
xhrSupported
=
jQuery
.
ajaxSettings
.
xhr
();
xhrCallbacks
=
{};
// Support: IE9
// Support: IE9
// Open requests must be manually aborted on unload (#5280)
// Open requests must be manually aborted on unload (#5280)
...
@@ -36,23 +36,29 @@ support.ajax = xhrSupported = !!xhrSupported;
...
@@ -36,23 +36,29 @@ support.ajax = xhrSupported = !!xhrSupported;
jQuery
.
ajaxTransport
(
function
(
options
)
{
jQuery
.
ajaxTransport
(
function
(
options
)
{
var
callback
;
var
callback
;
// Cross domain only allowed if supported through XMLHttpRequest
// Cross domain only allowed if supported through XMLHttpRequest
if
(
support
.
cors
||
xhrSupported
&&
!
options
.
crossDomain
)
{
if
(
support
.
cors
||
xhrSupported
&&
!
options
.
crossDomain
)
{
return
{
return
{
send
:
function
(
headers
,
complete
)
{
send
:
function
(
headers
,
complete
)
{
var
i
,
id
,
var
i
,
xhr
=
options
.
xhr
();
xhr
=
options
.
xhr
(),
id
=
++
xhrId
;
xhr
.
open
(
options
.
type
,
options
.
url
,
options
.
async
,
options
.
username
,
options
.
password
);
xhr
.
open
(
options
.
type
,
options
.
url
,
options
.
async
,
options
.
username
,
options
.
password
);
// Apply custom fields if provided
// Apply custom fields if provided
if
(
options
.
xhrFields
)
{
if
(
options
.
xhrFields
)
{
for
(
i
in
options
.
xhrFields
)
{
for
(
i
in
options
.
xhrFields
)
{
xhr
[
i
]
=
options
.
xhrFields
[
i
];
xhr
[
i
]
=
options
.
xhrFields
[
i
];
}
}
}
}
// Override mime type if needed
// Override mime type if needed
if
(
options
.
mimeType
&&
xhr
.
overrideMimeType
)
{
if
(
options
.
mimeType
&&
xhr
.
overrideMimeType
)
{
xhr
.
overrideMimeType
(
options
.
mimeType
);
xhr
.
overrideMimeType
(
options
.
mimeType
);
}
}
// X-Requested-With header
// X-Requested-With header
// For cross-domain requests, seeing as conditions for a preflight are
// For cross-domain requests, seeing as conditions for a preflight are
// akin to a jigsaw puzzle, we simply never set it to be sure.
// akin to a jigsaw puzzle, we simply never set it to be sure.
...
@@ -61,16 +67,19 @@ jQuery.ajaxTransport(function( options ) {
...
@@ -61,16 +67,19 @@ jQuery.ajaxTransport(function( options ) {
if
(
!
options
.
crossDomain
&&
!
headers
[
"X-Requested-With"
]
)
{
if
(
!
options
.
crossDomain
&&
!
headers
[
"X-Requested-With"
]
)
{
headers
[
"X-Requested-With"
]
=
"XMLHttpRequest"
;
headers
[
"X-Requested-With"
]
=
"XMLHttpRequest"
;
}
}
// Set headers
// Set headers
for
(
i
in
headers
)
{
for
(
i
in
headers
)
{
xhr
.
setRequestHeader
(
i
,
headers
[
i
]
);
xhr
.
setRequestHeader
(
i
,
headers
[
i
]
);
}
}
// Callback
// Callback
callback
=
function
(
type
)
{
callback
=
function
(
type
)
{
return
function
()
{
return
function
()
{
if
(
callback
)
{
if
(
callback
)
{
delete
xhrCallbacks
[
id
];
delete
xhrCallbacks
[
id
];
callback
=
xhr
.
onload
=
xhr
.
onerror
=
null
;
callback
=
xhr
.
onload
=
xhr
.
onerror
=
null
;
if
(
type
===
"abort"
)
{
if
(
type
===
"abort"
)
{
xhr
.
abort
();
xhr
.
abort
();
}
else
if
(
type
===
"error"
)
{
}
else
if
(
type
===
"error"
)
{
...
@@ -95,16 +104,20 @@ jQuery.ajaxTransport(function( options ) {
...
@@ -95,16 +104,20 @@ jQuery.ajaxTransport(function( options ) {
}
}
};
};
};
};
// Listen to events
// Listen to events
xhr
.
onload
=
callback
();
xhr
.
onload
=
callback
();
xhr
.
onerror
=
callback
(
"error"
);
xhr
.
onerror
=
callback
(
"error"
);
// Create the abort callback
// Create the abort callback
callback
=
xhrCallbacks
[(
id
=
xhrId
++
)]
=
callback
(
"abort"
);
callback
=
xhrCallbacks
[
id
]
=
callback
(
"abort"
);
// Do send the request
// Do send the request
// This may raise an exception which is actually
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
// handled in jQuery.ajax (so no try/catch here)
xhr
.
send
(
options
.
hasContent
&&
options
.
data
||
null
);
xhr
.
send
(
options
.
hasContent
&&
options
.
data
||
null
);
},
},
abort
:
function
()
{
abort
:
function
()
{
if
(
callback
)
{
if
(
callback
)
{
callback
();
callback
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment