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
b0ea80e9
Commit
b0ea80e9
authored
Apr 22, 2012
by
Richard Gibson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor jsonp.js for size and clarity
parent
d3b61de5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
51 deletions
+55
-51
jsonp.js
src/ajax/jsonp.js
+55
-51
No files found.
src/ajax/jsonp.js
View file @
b0ea80e9
(
function
(
jQuery
)
{
(
function
(
jQuery
)
{
var
jsc
=
jQuery
.
now
(),
var
oldCallbacks
=
[],
jsre
=
/
(\=)\?(
&|$
)
|
\?\?
/i
,
rquestion
=
/
\?
/
,
jscallbacks
=
[];
rjsonp
=
/
(
=
)\?(?=
&|$
)
|
\?\?
/
,
nonce
=
jQuery
.
now
();
// Default jsonp settings
// Default jsonp settings
jQuery
.
ajaxSetup
({
jQuery
.
ajaxSetup
({
jsonp
:
"callback"
,
jsonp
:
"callback"
,
jsonpCallback
:
function
()
{
jsonpCallback
:
function
()
{
var
callback
=
jscallbacks
.
pop
()
||
(
jQuery
.
expando
+
"_"
+
(
jsc
++
)
);
var
callback
=
oldCallbacks
.
pop
()
||
(
jQuery
.
expando
+
"_"
+
(
nonce
++
)
);
this
[
callback
]
=
true
;
this
[
callback
]
=
true
;
return
callback
;
return
callback
;
}
}
...
@@ -17,68 +18,71 @@ jQuery.ajaxSetup({
...
@@ -17,68 +18,71 @@ jQuery.ajaxSetup({
// Detect, normalize options and install callbacks for jsonp requests
// Detect, normalize options and install callbacks for jsonp requests
jQuery
.
ajaxPrefilter
(
"json jsonp"
,
function
(
s
,
originalSettings
,
jqXHR
)
{
jQuery
.
ajaxPrefilter
(
"json jsonp"
,
function
(
s
,
originalSettings
,
jqXHR
)
{
var
inspectData
=
(
typeof
s
.
data
===
"string"
)
&&
/^application
\/
x
\-
www
\-
form
\-
urlencoded/
.
test
(
s
.
contentType
);
var
callbackName
,
overwritten
,
responseContainer
,
data
=
s
.
data
,
url
=
s
.
url
,
hasCallback
=
s
.
jsonp
!==
false
,
replaceInUrl
=
hasCallback
&&
rjsonp
.
test
(
url
),
replaceInData
=
hasCallback
&&
typeof
data
===
"string"
&&
!
(
s
.
contentType
||
""
).
indexOf
(
"application/x-www-form-urlencoded"
)
&&
rjsonp
.
test
(
data
);
if
(
s
.
dataTypes
[
0
]
===
"jsonp"
||
// Handle iff the expected data type is "jsonp" or we have a parameter to set
s
.
jsonp
!==
false
&&
(
jsre
.
test
(
s
.
url
)
||
if
(
s
.
dataTypes
[
0
]
===
"jsonp"
||
hasCallback
&&
inspectData
&&
jsre
.
test
(
s
.
data
)
)
)
{
(
replaceInUrl
||
replaceInData
)
)
{
var
responseContainer
,
// Get callback name, remembering preexisting value associated with it
jsonpCallback
=
s
.
jsonpCallback
=
callbackName
=
s
.
jsonpCallback
=
jQuery
.
isFunction
(
s
.
jsonpCallback
)
?
jQuery
.
isFunction
(
s
.
jsonpCallback
)
?
s
.
jsonpCallback
()
:
s
.
jsonpCallback
,
s
.
jsonpCallback
()
:
previous
=
window
[
jsonpCallback
],
s
.
jsonpCallback
;
url
=
s
.
url
,
overwritten
=
window
[
callbackName
];
data
=
s
.
data
,
replace
=
"$1"
+
jsonpCallback
+
"$2"
;
if
(
s
.
jsonp
!==
false
)
{
// Insert callback into url or form data
url
=
url
.
replace
(
jsre
,
replace
);
if
(
replaceInUrl
)
{
if
(
s
.
url
===
url
)
{
s
.
url
=
url
.
replace
(
rjsonp
,
"$1"
+
callbackName
);
if
(
inspectData
)
{
}
else
if
(
replaceInData
)
{
data
=
data
.
replace
(
jsre
,
replace
);
s
.
data
=
data
.
replace
(
rjsonp
,
"$1"
+
callbackName
);
}
}
else
if
(
hasCallback
)
{
if
(
s
.
data
===
data
)
{
s
.
url
+=
(
rquestion
.
test
(
url
)
?
"&"
:
"?"
)
+
s
.
jsonp
+
"="
+
callbackName
;
// Add callback manually
url
+=
(
/
\?
/
.
test
(
url
)
?
"&"
:
"?"
)
+
s
.
jsonp
+
"="
+
jsonpCallback
;
}
}
}
// Use data converter to retrieve json after script execution
s
.
converters
[
"script json"
]
=
function
()
{
if
(
!
responseContainer
)
{
jQuery
.
error
(
callbackName
+
" was not called"
);
}
}
return
responseContainer
[
0
];
};
s
.
url
=
url
;
// force json dataType
s
.
data
=
data
;
s
.
data
Types
[
0
]
=
"json"
;
// Install callback
// Install callback
window
[
jsonpCallback
]
=
function
(
response
)
{
window
[
callbackName
]
=
function
(
)
{
responseContainer
=
[
response
]
;
responseContainer
=
arguments
;
};
};
// Clean-up function
// Clean-up function
(fires after converters)
jqXHR
.
always
(
function
()
{
jqXHR
.
always
(
function
()
{
// Set callback back to previous value
// Restore preexisting value
window
[
jsonpCallback
]
=
previous
;
window
[
callbackName
]
=
overwritten
;
// Save back as free
// Save back as free
if
(
s
[
jsonpCallback
]
)
{
if
(
s
[
callbackName
]
)
{
// make sure that re-using the options doesn't screw things around
// make sure that re-using the options doesn't screw things around
s
.
jsonpCallback
=
originalSettings
.
jsonpCallback
;
s
.
jsonpCallback
=
originalSettings
.
jsonpCallback
;
// save the callback name for future use
// save the callback name for future use
jscallbacks
.
push
(
jsonpCallback
);
oldCallbacks
.
push
(
callbackName
);
}
// Call if it was a function and we have a response
if
(
responseContainer
&&
jQuery
.
isFunction
(
previous
)
)
{
window
[
jsonpCallback
](
responseContainer
[
0
]
);
}
}
});
// Use data converter to retrieve json after script execution
// Call if it was a function and we have a response
s
.
converters
[
"script json"
]
=
function
()
{
if
(
responseContainer
&&
jQuery
.
isFunction
(
overwritten
)
)
{
if
(
!
responseContainer
)
{
overwritten
(
responseContainer
[
0
]
);
jQuery
.
error
(
jsonpCallback
+
" was not called"
);
}
}
return
responseContainer
[
0
];
};
// force json dataType
responseContainer
=
overwritten
=
undefined
;
s
.
dataTypes
[
0
]
=
"json"
;
})
;
// Delegate to script
// Delegate to script
return
"script"
;
return
"script"
;
...
...
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