views:

102

answers:

2

Hi, I'm trying to write a jquery interface which requires me to pass a couple of parameters to our CMS. These parameters are in the form "attribute[n]:token" so in a URL you'd end up with "...&attribute[1]:value=hello_world...". Unfortunately when I try to use a $.get to pass this data it chokes for fairly obvious reasons. I'm tried the methods I can think of to escape these character and and I'm sure I'm missing a simple trick but I can't come up with a method which works. I hope this isn't as simple a question as it sounds.

example code:

    $.get("/example.htm", 
 {
  Attributes[1]:type: "option",
  Attributes[1]:value: "large"
 });

many thanks in advance, Sam

+1  A: 

you can use the javascript function encodeURI():

var params = {};
params[encodeURI('Attributes[1]:type')] = 'option';
params[encodeURI('Attributes[1]:value')] = 'large';

$.get("/example.htm", params);
Lior Cohen
Lior, Many thanks. I'd tried encodeURI but I suspect that I'd made a trivial error and then dismissed it as a bad job. Actually it turns out that simply making the array instead of passing the variables inline was enough to solve my problem. I knew I was being dense.Thanks again,Sam
Sam Cartwright
np mate. it happens :)
Lior Cohen
A: 

You could always try adding quotes around the keys

$.get("/example.htm", 
{
    "Attributes[1]:type": "option",
    "Attributes[1]:value": "large"
});
Marco