Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add jQuery.isPlainObject check to ensure constructed property values are not misinterpretted as objects. Fixes #10466 #540

Closed
wants to merge 2 commits into from

3 participants

@rwaldron
Collaborator

Add jQuery.isPlainObject check to ensure constructed property values are not misinterpretted as objects. Fixes #10466

http://bugs.jquery.com/ticket/10466

@dmethvin

This isn't likely to be a perf-critical path, so isPlainObject alone would be shorter. Also, all other constructed non-plain objects (e.g., Date) will now be stringified in param() by encodeURIComponent, dunno if that changes their behavior as well.

Hmm, seems like the obj != null test is redundant if we use isPlainObject.

Collaborator

do you need the extra parens? aren't these all AND'ed?

Collaborator

Why are you reviewing the first commit and not the latest?

Collaborator

Why are you reviewing the first commit and not the latest?

@rwldrn, Because I'm a jerk.

Collaborator

hahahah hilarious :)

@rwaldron
Collaborator

I'll test that and update the branch

@dmethvin
Owner

Landed. 166b9d2

@dmethvin dmethvin closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 10, 2011
  1. @rwaldron

    Add jQuery.isPlainObject check to ensure constructed property values …

    rwaldron authored
    …are not misinterpretted as objects. Fixes #10466
  2. @rwaldron
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 1 deletion.
  1. +1 −1  src/ajax.js
  2. +13 −0 test/unit/ajax.js
View
2  src/ajax.js
@@ -818,7 +818,7 @@ function buildParams( prefix, obj, traditional, add ) {
}
});
- } else if ( !traditional && obj != null && typeof obj === "object" ) {
+ } else if ( !traditional && jQuery.isPlainObject( obj ) ) {
// Serialize object item.
for ( var name in obj ) {
buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
View
13 test/unit/ajax.js
@@ -1031,6 +1031,19 @@ test("jQuery.param()", function() {
equals( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
});
+test("jQuery.param() Constructed prop values", function() {
+ expect(3);
+
+ var params = {"test": new String("foo") };
+ equal( jQuery.param( params, false ), "test=foo", "Do not mistake new String() for a plain object" );
+
+ params = {"test": new Number(5) };
+ equal( jQuery.param( params, false ), "test=5", "Do not mistake new Number() for a plain object" );
+
+ params = {"test": new Date() };
+ ok( jQuery.param( params, false ), "(Non empty string returned) Do not mistake new Date() for a plain object" );
+});
+
test("synchronous request", function() {
expect(1);
ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
Something went wrong with that request. Please try again.