473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

send json as ajax request to server

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

Aug 14 '07 #1
2 11420
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

Aug 14 '07 #2
thank you for your responce. i like the function you wrote there. It's
simple and clean.

On Aug 13, 9:07 pm, David Golightly <davig...@gmail.comwrote:
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

Aug 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: trpost | last post by:
I am looking to make a small web app that will return the status of a website from the client browser. I tried this with AJAX and it worked great locally, but did not work for remote users...
9
by: Mahernoz | last post by:
Can i send an email from JavaScript? Is it possible? If yes please the code to send email using javascript...
5
by: Otto Wyss | last post by:
I've now been looking for a week for a simple but useful sample on how to get a list of entries (persons) via an XMLHttpRequest using Json/PHP on the server. So far I've found about a thousend...
5
by: Jimmy | last post by:
How can I do that? Is it possible to send a server request (i.e. GET) without refreshing the web page? Using Javascript? Or Ajax (i.e. AjaxAnywhere)? Or if there's a way (i.e. using...
1
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
3
by: xhe | last post by:
I found Jason is a very handy data format for client/server communication. But I just met a problem as follows: I want to read the data replied from server in Jason format, the reply is like...
1
by: Jeff | last post by:
I'm writing my first json/ajax code and I'm having a hard time wrapping my mind around security issues. I'm thinking of a json response that would look like this: {"data":}...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.