On Aug 13, 5:15 pm, giloosh <giloos...@gmail.comwrote:
whats the best way to pass a json string to the server.
if my jsonstring = {a:'1',b:'sds',c:'sdg'}
could i send that to the server passing it as 1 variable like so:
url = /serverside.php?json=jsonstring
or would i have to break it down into something like this:
url = /serverside.php?a=1&b=sds&c=sdg
Send it as a POST parameter. In fact, that's the best general rule
for any info you're trying to send to the server (especially large
amounts) and expecting the server to record somewhere (presumably in a
database).
But for a small number of simple names/values like you've got there,
especially if the action isn't a "write" but a "read", you could get
away with using GET query parameters; in fact, that would be the best
choice in this situation, as it saves having to parse a JSON POST into
a server-side data structure.
Something like:
function toURLString(o) {
var a = [];
for (var n in o) {
a[a.length] = encodeURIComponent(n) + '=' +
encodeURIComponent(o[n]);
}
return a.join('&');
}
should get you what you're looking for, if all you're working with are
flat objects without nested arrays or anything like that.
-David