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
50d78e76
Commit
50d78e76
authored
Sep 02, 2009
by
Yehuda Katz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds nested param serialization; Closes #4201 (by merbjedi)
parent
569c8b45
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
12 deletions
+30
-12
ajax.js
src/ajax.js
+16
-11
ajax.js
test/unit/ajax.js
+14
-1
No files found.
src/ajax.js
View file @
50d78e76
...
...
@@ -588,23 +588,28 @@ jQuery.extend({
// of form elements
if
(
jQuery
.
isArray
(
a
)
||
a
.
jquery
)
{
// Serialize the form elements
jQuery
.
each
(
a
,
function
(){
jQuery
.
each
(
a
,
function
()
{
add
(
this
.
name
,
this
.
value
);
});
// Otherwise, assume that it's an object of key/value pairs
}
else
{
// Serialize the key/values
for
(
var
j
in
a
)
{
// If the value is an array then the key names need to be repeated
if
(
jQuery
.
isArray
(
a
[
j
])
)
{
jQuery
.
each
(
a
[
j
],
function
(){
add
(
j
,
this
);
});
// Recursively encode parameters from object,
// building a prefix path as we go down
function
buildParams
(
obj
,
prefix
)
{
if
(
jQuery
.
isArray
(
obj
)
)
{
for
(
var
i
=
0
,
length
=
obj
.
length
;
i
<
length
;
i
++
)
{
buildParams
(
obj
[
i
],
prefix
);
};
}
else
if
(
typeof
(
obj
)
==
"object"
)
{
for
(
var
j
in
obj
)
{
var
postfix
=
((
j
.
indexOf
(
"[]"
)
>
0
)
?
"[]"
:
""
);
// move any brackets to the end
buildParams
(
obj
[
j
],
(
prefix
?
(
prefix
+
"["
+
j
.
replace
(
"[]"
,
""
)
+
"]"
+
postfix
)
:
j
)
);
}
}
else
{
add
(
j
,
jQuery
.
isFunction
(
a
[
j
])
?
a
[
j
]()
:
a
[
j
]
);
add
(
prefix
,
jQuery
.
isFunction
(
obj
)
?
obj
()
:
obj
);
}
}
buildParams
(
a
);
}
// Return the resulting serialization
...
...
test/unit/ajax.js
View file @
50d78e76
...
...
@@ -213,18 +213,31 @@ test("serialize()", function() {
});
test
(
"jQuery.param()"
,
function
()
{
expect
(
4
);
expect
(
8
);
var
params
=
{
foo
:
"bar"
,
baz
:
42
,
quux
:
"All your base are belong to us"
};
equals
(
jQuery
.
param
(
params
),
"foo=bar&baz=42&quux=All+your+base+are+belong+to+us"
,
"simple"
);
params
=
{
someName
:
[
1
,
2
,
3
],
regularThing
:
"blah"
};
equals
(
jQuery
.
param
(
params
),
"someName=1&someName=2&someName=3®ularThing=blah"
,
"with array"
);
params
=
{
foo
:
[
'a'
,
'b'
,
'c'
]};
equals
(
jQuery
.
param
(
params
),
"foo=a&foo=b&foo=c"
,
"with array of strings"
);
params
=
{
"foo[]"
:[
"baz"
,
42
,
"All your base are belong to us"
]};
equals
(
jQuery
.
param
(
params
),
"foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us"
,
"more array"
);
params
=
{
"foo[bar]"
:
"baz"
,
"foo[beep]"
:
42
,
"foo[quux]"
:
"All your base are belong to us"
};
equals
(
jQuery
.
param
(
params
),
"foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us"
,
"even more arrays"
);
params
=
{
foo
:
{
bar
:
"baz"
,
beep
:
42
,
quux
:
"All your base are belong to us"
}};
equals
(
jQuery
.
param
(
params
),
"foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us"
,
"nested object"
);
params
=
{
foo
:
{
"bar[]"
:
[
1
,
2
,
3
]}};
equals
(
jQuery
.
param
(
params
),
"foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3"
,
"nested array"
);
params
=
{
foo
:
[{
bar
:
"baz"
,
beep
:
42
},
{
bar
:
"baz2"
,
beep
:
43
}]};
equals
(
jQuery
.
param
(
params
),
"foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43"
,
"nested array of objects"
);
});
test
(
"synchronous request"
,
function
()
{
...
...
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