I am using this format to pass data to server in GET request .
var val = {
name:"abcd",
age="21"
}
var val2 = "test2" ;
http://server-name/getdata.htm?data=JSON.stringify(val)&data1=val2
.
This works fine but , when val.name is like “abcd&def”
the format of request gets distorted due to this “&”
What should I do ?
You should correctly encode the elements of the query string, using
encodeURIComponent(that’s a link to MDC, but the function is available in all browsers, not just Firefox).You haven’t shown actual code, but along these lines:
Technically, the more correct way would be to also encode the keys
dataanddata1, like this:…but when you’re dealing with literal keys (as opposed to keys coming from strings you don’t control), when you know the encoded form is identical to the original (which it is for
dataanddata1), you can get away with not encoding the keys.