send json as ajax request to server

G

giloosh

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
 
D

David Golightly

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
 
G

giloosh

thank you for your responce. i like the function you wrote there. It's
simple and clean.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top