Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$.param($.deparam('foo[10000000]=bar')); // breaks the world #20

Open
brian-gates opened this issue Sep 24, 2010 · 2 comments
Open

$.param($.deparam('foo[10000000]=bar')); // breaks the world #20

brian-gates opened this issue Sep 24, 2010 · 2 comments

Comments

@brian-gates
Copy link

See the subject. I'm not sure if this is really a bug, per se, but it's certainly an unintuitive problem.

In my particular situation I'm working with a form that has a key that represents a very high-value incremental id in the database. Serializing it provides a string not unlike the value in the subject.

The problem starts here:
$.deparam('foo[10000000]=bar'); // [undefined, undefined, undefined, 9999998 more...]

Converting this data back to a string will break the browser.

Working with objects would resolve this problem:

$.deparam('foo[10000000]=bar'); // { foo: {'1000': 'bar'} }

Not sure if this has any other functional implications, however.

@Gamesh
Copy link

Gamesh commented Mar 23, 2011

i have the same problem, when merging hash with key value like 123 the hash put in the url is many undefined parameters up to 122 hashes.
ex.: param[123]=1
if pushed creates 122 param with value undefined, and the last one is our param[123]=1

@jordoh
Copy link

jordoh commented Nov 22, 2012

I just ran into the same issue with Rails' query string output for a hash with a numerical key and an array as the associated value. Passing hash[100][]=val to deparam with a numerical key would result in { hash: [ <100 nil entries>, "val" ] }.

I was able to work around it by pre-processing the params string so hash[1][]=a becomes hash['1'][]=a, which produces the desired { hash: { '1': [ "a" ] } }. Here's a bit of code that could be adapted to work without the empty brackets Rails adds:

var prepareParams = function(params) {
  return params.replace(/\[(\d+)\]\[\]=/ig, function(match, key) {
    return "['" + key + "'][]=";
  });
};

var params = $.deparam.querystring(prepareParams(window.location.href));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants