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
f91b944c
Commit
f91b944c
authored
Jan 25, 2010
by
John Resig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Objects with length properties weren't getting serialized properly by jQuery.param(). Fixes #5862.
parent
76236a15
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
40 deletions
+54
-40
ajax.js
src/ajax.js
+41
-40
ajax.js
test/unit/ajax.js
+13
-0
No files found.
src/ajax.js
View file @
f91b944c
...
@@ -603,7 +603,6 @@ jQuery.extend({
...
@@ -603,7 +603,6 @@ jQuery.extend({
// Serialize an array of form elements or a set of
// Serialize an array of form elements or a set of
// key/values into a query string
// key/values into a query string
param
:
function
(
a
,
traditional
)
{
param
:
function
(
a
,
traditional
)
{
var
s
=
[];
var
s
=
[];
// Set traditional to true for jQuery <= 1.3.2 behavior.
// Set traditional to true for jQuery <= 1.3.2 behavior.
...
@@ -611,12 +610,6 @@ jQuery.extend({
...
@@ -611,12 +610,6 @@ jQuery.extend({
traditional
=
jQuery
.
ajaxSettings
.
traditional
;
traditional
=
jQuery
.
ajaxSettings
.
traditional
;
}
}
function
add
(
key
,
value
)
{
// If value is a function, invoke it and return its value
value
=
jQuery
.
isFunction
(
value
)
?
value
()
:
value
;
s
[
s
.
length
]
=
encodeURIComponent
(
key
)
+
"="
+
encodeURIComponent
(
value
);
}
// If an array was passed in, assume that it is an array of form elements.
// If an array was passed in, assume that it is an array of form elements.
if
(
jQuery
.
isArray
(
a
)
||
a
.
jquery
)
{
if
(
jQuery
.
isArray
(
a
)
||
a
.
jquery
)
{
// Serialize the form elements
// Serialize the form elements
...
@@ -627,41 +620,49 @@ jQuery.extend({
...
@@ -627,41 +620,49 @@ jQuery.extend({
}
else
{
}
else
{
// If traditional, encode the "old" way (the way 1.3.2 or older
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
// did it), otherwise encode params recursively.
jQuery
.
each
(
a
,
function
buildParams
(
prefix
,
obj
)
{
for
(
var
prefix
in
a
)
{
buildParams
(
prefix
,
a
[
prefix
]
);
if
(
jQuery
.
isArray
(
obj
)
)
{
}
// Serialize array item.
jQuery
.
each
(
obj
,
function
(
i
,
v
)
{
if
(
traditional
)
{
// Treat each array item as a scalar.
add
(
prefix
,
v
);
}
else
{
// If array item is non-scalar (array or object), encode its
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams
(
prefix
+
"["
+
(
typeof
v
===
"object"
||
jQuery
.
isArray
(
v
)
?
i
:
""
)
+
"]"
,
v
);
}
});
}
else
if
(
!
traditional
&&
obj
!=
null
&&
typeof
obj
===
"object"
)
{
// Serialize object item.
jQuery
.
each
(
obj
,
function
(
k
,
v
)
{
buildParams
(
prefix
+
"["
+
k
+
"]"
,
v
);
});
}
else
{
// Serialize scalar item.
add
(
prefix
,
obj
);
}
});
}
}
// Return the resulting serialization
// Return the resulting serialization
return
s
.
join
(
"&"
).
replace
(
r20
,
"+"
);
return
s
.
join
(
"&"
).
replace
(
r20
,
"+"
);
}
function
buildParams
(
prefix
,
obj
)
{
if
(
jQuery
.
isArray
(
obj
)
)
{
// Serialize array item.
jQuery
.
each
(
obj
,
function
(
i
,
v
)
{
if
(
traditional
)
{
// Treat each array item as a scalar.
add
(
prefix
,
v
);
}
else
{
// If array item is non-scalar (array or object), encode its
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams
(
prefix
+
"["
+
(
typeof
v
===
"object"
||
jQuery
.
isArray
(
v
)
?
i
:
""
)
+
"]"
,
v
);
}
});
}
else
if
(
!
traditional
&&
obj
!=
null
&&
typeof
obj
===
"object"
)
{
// Serialize object item.
jQuery
.
each
(
obj
,
function
(
k
,
v
)
{
buildParams
(
prefix
+
"["
+
k
+
"]"
,
v
);
});
}
else
{
// Serialize scalar item.
add
(
prefix
,
obj
);
}
}
function
add
(
key
,
value
)
{
// If value is a function, invoke it and return its value
value
=
jQuery
.
isFunction
(
value
)
?
value
()
:
value
;
s
[
s
.
length
]
=
encodeURIComponent
(
key
)
+
"="
+
encodeURIComponent
(
value
);
}
}
});
});
test/unit/ajax.js
View file @
f91b944c
...
@@ -979,6 +979,19 @@ test("jQuery.getJSON(String, Function) - JSON object with absolute url to local
...
@@ -979,6 +979,19 @@ test("jQuery.getJSON(String, Function) - JSON object with absolute url to local
});
});
});
});
test
(
"jQuery.post - data"
,
function
()
{
expect
(
2
);
stop
();
jQuery
.
post
(
url
(
"data/name.php"
),
{
xml
:
"5-2"
,
length
:
3
},
function
(
xml
){
jQuery
(
'math'
,
xml
).
each
(
function
()
{
equals
(
jQuery
(
'calculation'
,
this
).
text
(),
'5-2'
,
'Check for XML'
);
equals
(
jQuery
(
'result'
,
this
).
text
(),
'3'
,
'Check for XML'
);
});
start
();
});
});
test
(
"jQuery.post(String, Hash, Function) - simple with xml"
,
function
()
{
test
(
"jQuery.post(String, Hash, Function) - simple with xml"
,
function
()
{
expect
(
4
);
expect
(
4
);
stop
();
stop
();
...
...
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