473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11437
On Aug 13, 5:15 pm, giloosh <giloos...@gmai l.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] = encodeURICompon ent(n) + '=' +
encodeURICompon ent(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...@gmai l.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] = encodeURICompon ent(n) + '=' +
encodeURICompon ent(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
12397
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 accessing the page, I ran into the security problem with making a cross domain request. I have been reading that with JSON a cross domain request can be accomplished, but have not been able to find any examples on how to use it or how to get the return...
9
77421
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
16121
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 different tutorials and code samples but not a single one, where the server returns an array of entries. Very few samples use Json at all and almost none show the server code. So does anybody know a sample which - uses just a small javascript...
5
3122
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 AjaxAnywhere) to send the 1st GET request without updating any zone to server #1? If so, then I can send the 2nd GET request to server #2 and update a particular zone in the web page. Thanks,
1
2286
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 would like to read the JSON string sent from the client on the
3
2285
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 this: it is generated automatically by amfphp1.9 from an array. I used Ajax to call a method, and the server code replied an array in the above
1
1903
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":} ,{"name":"name2","street":"street2"} ], "instructions":{"function_to_execute":"some_function"} }
1
2668
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 need to specifiy the index key and I can treat it exactly like an array - oSet = ... Are objects just as quick to access/alter as arrays? I have a few
0
5369
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 you an example of how an data class (actually, a list of PHP documentation pages) might be represented, first in XML. and then in JSON. This side-by-side comparison should let you begin to understand how to represent data in JSON. The XML version:...
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7928
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6735
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.