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
45a45599
Commit
45a45599
authored
Nov 03, 2013
by
Richard Gibson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ref
705216dc
: Synchronize ajax/xhr.js with master. Close gh-1418.
parent
faed51b2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
76 deletions
+54
-76
xhr.js
src/ajax/xhr.js
+54
-76
No files found.
src/ajax/xhr.js
View file @
45a45599
...
@@ -4,30 +4,6 @@ define([
...
@@ -4,30 +4,6 @@ define([
"../ajax"
"../ajax"
],
function
(
jQuery
,
support
)
{
],
function
(
jQuery
,
support
)
{
var
xhrCallbacks
,
xhrSupported
,
xhrId
=
0
,
// #5280: Internet Explorer will keep connections alive if we don't abort on unload
xhrOnUnloadAbort
=
window
.
ActiveXObject
&&
function
()
{
// Abort all pending requests
var
key
;
for
(
key
in
xhrCallbacks
)
{
xhrCallbacks
[
key
](
undefined
,
true
);
}
};
// Functions to create xhrs
function
createStandardXHR
()
{
try
{
return
new
window
.
XMLHttpRequest
();
}
catch
(
e
)
{}
}
function
createActiveXHR
()
{
try
{
return
new
window
.
ActiveXObject
(
"Microsoft.XMLHTTP"
);
}
catch
(
e
)
{}
}
// Create the request object
// Create the request object
// (This is still attached to ajaxSettings for backward compatibility)
// (This is still attached to ajaxSettings for backward compatibility)
jQuery
.
ajaxSettings
.
xhr
=
window
.
ActiveXObject
?
jQuery
.
ajaxSettings
.
xhr
=
window
.
ActiveXObject
?
...
@@ -43,45 +19,52 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
...
@@ -43,45 +19,52 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
// For all other browsers, use the standard XMLHttpRequest object
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR
;
createStandardXHR
;
var
xhrId
=
0
,
xhrCallbacks
=
{},
xhrSupported
=
jQuery
.
ajaxSettings
.
xhr
();
// Support: IE<10
// Open requests must be manually aborted on unload (#5280)
if
(
window
.
ActiveXObject
)
{
jQuery
(
window
).
on
(
"unload"
,
function
()
{
for
(
var
key
in
xhrCallbacks
)
{
xhrCallbacks
[
key
](
undefined
,
true
);
}
});
}
// Determine support properties
// Determine support properties
xhrSupported
=
jQuery
.
ajaxSettings
.
xhr
();
support
.
cors
=
!!
xhrSupported
&&
(
"withCredentials"
in
xhrSupported
);
support
.
cors
=
!!
xhrSupported
&&
(
"withCredentials"
in
xhrSupported
);
xhrSupported
=
support
.
ajax
=
!!
xhrSupported
;
xhrSupported
=
support
.
ajax
=
!!
xhrSupported
;
// Create transport if the browser can provide an xhr
// Create transport if the browser can provide an xhr
if
(
xhrSupported
)
{
if
(
xhrSupported
)
{
jQuery
.
ajaxTransport
(
function
(
s
)
{
jQuery
.
ajaxTransport
(
function
(
option
s
)
{
// Cross domain only allowed if supported through XMLHttpRequest
// Cross domain only allowed if supported through XMLHttpRequest
if
(
!
s
.
crossDomain
||
support
.
cors
)
{
if
(
!
option
s
.
crossDomain
||
support
.
cors
)
{
var
callback
;
var
callback
;
return
{
return
{
send
:
function
(
headers
,
complete
)
{
send
:
function
(
headers
,
complete
)
{
var
i
,
// Get a new xhr
xhr
=
options
.
xhr
(),
var
handle
,
i
,
id
=
++
xhrId
;
xhr
=
s
.
xhr
();
// Open the socket
// Open the socket
// Passing null username, generates a login popup on Opera (#2865)
xhr
.
open
(
options
.
type
,
options
.
url
,
options
.
async
,
options
.
username
,
options
.
password
);
if
(
s
.
username
)
{
xhr
.
open
(
s
.
type
,
s
.
url
,
s
.
async
,
s
.
username
,
s
.
password
);
}
else
{
xhr
.
open
(
s
.
type
,
s
.
url
,
s
.
async
);
}
// Apply custom fields if provided
// Apply custom fields if provided
if
(
s
.
xhrFields
)
{
if
(
option
s
.
xhrFields
)
{
for
(
i
in
s
.
xhrFields
)
{
for
(
i
in
option
s
.
xhrFields
)
{
xhr
[
i
]
=
s
.
xhrFields
[
i
];
xhr
[
i
]
=
option
s
.
xhrFields
[
i
];
}
}
}
}
// Override mime type if needed
// Override mime type if needed
if
(
s
.
mimeType
&&
xhr
.
overrideMimeType
)
{
if
(
option
s
.
mimeType
&&
xhr
.
overrideMimeType
)
{
xhr
.
overrideMimeType
(
s
.
mimeType
);
xhr
.
overrideMimeType
(
option
s
.
mimeType
);
}
}
// X-Requested-With header
// X-Requested-With header
...
@@ -89,11 +72,11 @@ if ( xhrSupported ) {
...
@@ -89,11 +72,11 @@ if ( xhrSupported ) {
// 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.
// (it can always be set on a per-request basis or even using ajaxSetup)
// (it can always be set on a per-request basis or even using ajaxSetup)
// For same-domain requests, won't change header if already provided.
// For same-domain requests, won't change header if already provided.
if
(
!
s
.
crossDomain
&&
!
headers
[
"X-Requested-With"
]
)
{
if
(
!
option
s
.
crossDomain
&&
!
headers
[
"X-Requested-With"
]
)
{
headers
[
"X-Requested-With"
]
=
"XMLHttpRequest"
;
headers
[
"X-Requested-With"
]
=
"XMLHttpRequest"
;
}
}
// Need an extra try/catch for cross
domain requests in Firefox 3
// Need an extra try/catch for cross
-
domain requests in Firefox 3
try
{
try
{
for
(
i
in
headers
)
{
for
(
i
in
headers
)
{
xhr
.
setRequestHeader
(
i
,
headers
[
i
]
);
xhr
.
setRequestHeader
(
i
,
headers
[
i
]
);
...
@@ -103,11 +86,11 @@ if ( xhrSupported ) {
...
@@ -103,11 +86,11 @@ if ( xhrSupported ) {
// 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
(
(
s
.
hasContent
&&
s
.
data
)
||
null
);
xhr
.
send
(
(
options
.
hasContent
&&
option
s
.
data
)
||
null
);
// Listener
// Listener
callback
=
function
(
_
,
isAbort
)
{
callback
=
function
(
_
,
isAbort
)
{
var
status
,
responseHeaders
,
statusText
,
responses
;
var
status
,
statusText
,
responses
;
// Firefox throws exceptions when accessing properties
// Firefox throws exceptions when accessing properties
// of an xhr when a network error occurred
// of an xhr when a network error occurred
...
@@ -116,31 +99,23 @@ if ( xhrSupported ) {
...
@@ -116,31 +99,23 @@ if ( xhrSupported ) {
// Was never called and is aborted or complete
// Was never called and is aborted or complete
if
(
callback
&&
(
isAbort
||
xhr
.
readyState
===
4
)
)
{
if
(
callback
&&
(
isAbort
||
xhr
.
readyState
===
4
)
)
{
// Clean up
// Only called once
delete
xhrCallbacks
[
id
];
callback
=
undefined
;
callback
=
undefined
;
xhr
.
onreadystatechange
=
jQuery
.
noop
;
// Do not keep as active anymore
// Abort manually if needed
if
(
handle
)
{
xhr
.
onreadystatechange
=
jQuery
.
noop
;
if
(
xhrOnUnloadAbort
)
{
delete
xhrCallbacks
[
handle
];
}
}
// If it's an abort
if
(
isAbort
)
{
if
(
isAbort
)
{
// Abort it manually if needed
if
(
xhr
.
readyState
!==
4
)
{
if
(
xhr
.
readyState
!==
4
)
{
xhr
.
abort
();
xhr
.
abort
();
}
}
}
else
{
}
else
{
responses
=
{};
responses
=
{};
status
=
xhr
.
status
;
status
=
xhr
.
status
;
responseHeaders
=
xhr
.
getAllResponseHeaders
();
// When requesting binary data, IE6-9 will throw an exception
// Support: IE<10
// on any attempt to access responseText (#11426)
// #11426: When requesting binary data, IE9 will throw an exception
// on any attempt to access responseText
if
(
typeof
xhr
.
responseText
===
"string"
)
{
if
(
typeof
xhr
.
responseText
===
"string"
)
{
responses
.
text
=
xhr
.
responseText
;
responses
.
text
=
xhr
.
responseText
;
}
}
...
@@ -159,7 +134,7 @@ if ( xhrSupported ) {
...
@@ -159,7 +134,7 @@ if ( xhrSupported ) {
// If the request is local and we have data: assume a success
// If the request is local and we have data: assume a success
// (success with no data won't get notified, that's the best we
// (success with no data won't get notified, that's the best we
// can do given current implementations)
// can do given current implementations)
if
(
!
status
&&
s
.
isLocal
&&
!
s
.
crossDomain
)
{
if
(
!
status
&&
options
.
isLocal
&&
!
option
s
.
crossDomain
)
{
status
=
responses
.
text
?
200
:
404
;
status
=
responses
.
text
?
200
:
404
;
// IE - #1450: sometimes returns 1223 when it should be 204
// IE - #1450: sometimes returns 1223 when it should be 204
}
else
if
(
status
===
1223
)
{
}
else
if
(
status
===
1223
)
{
...
@@ -175,11 +150,11 @@ if ( xhrSupported ) {
...
@@ -175,11 +150,11 @@ if ( xhrSupported ) {
// Call complete if needed
// Call complete if needed
if
(
responses
)
{
if
(
responses
)
{
complete
(
status
,
statusText
,
responses
,
responseHeaders
);
complete
(
status
,
statusText
,
responses
,
xhr
.
getAllResponseHeaders
()
);
}
}
};
};
if
(
!
s
.
async
)
{
if
(
!
option
s
.
async
)
{
// if we're in sync mode we fire the callback
// if we're in sync mode we fire the callback
callback
();
callback
();
}
else
if
(
xhr
.
readyState
===
4
)
{
}
else
if
(
xhr
.
readyState
===
4
)
{
...
@@ -187,18 +162,8 @@ if ( xhrSupported ) {
...
@@ -187,18 +162,8 @@ if ( xhrSupported ) {
// retrieved directly we need to fire the callback
// retrieved directly we need to fire the callback
setTimeout
(
callback
);
setTimeout
(
callback
);
}
else
{
}
else
{
handle
=
++
xhrId
;
// Add to the list of active xhr callbacks
if
(
xhrOnUnloadAbort
)
{
xhr
.
onreadystatechange
=
xhrCallbacks
[
id
]
=
callback
;
// Create the active xhrs callbacks list if needed
// and attach the unload handler
if
(
!
xhrCallbacks
)
{
xhrCallbacks
=
{};
jQuery
(
window
).
unload
(
xhrOnUnloadAbort
);
}
// Add to list of active xhrs callbacks
xhrCallbacks
[
handle
]
=
callback
;
}
xhr
.
onreadystatechange
=
callback
;
}
}
},
},
...
@@ -212,4 +177,17 @@ if ( xhrSupported ) {
...
@@ -212,4 +177,17 @@ if ( xhrSupported ) {
});
});
}
}
// Functions to create xhrs
function
createStandardXHR
()
{
try
{
return
new
window
.
XMLHttpRequest
();
}
catch
(
e
)
{}
}
function
createActiveXHR
()
{
try
{
return
new
window
.
ActiveXObject
(
"Microsoft.XMLHTTP"
);
}
catch
(
e
)
{}
}
});
});
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