473,545 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does one handle JSON strings from the browser

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
server side.

Here is the Javascript which show: [this is a code snippet only]
1. creating an object
2. encoding to JSON string
3. making the ajax request.

-----------------------------------------------------------------------------------------------------------------
12 function makeRequest() {
13 var jsonObj;
14 var transaction;
15 var jstr;
16
17 jsonObj = {
18 action : "ack",
19 var1 : "fly"
20 };
21
22 jstr = jsonObj.toJSONS tring();
23 transaction =
YAHOO.util.Conn ect.asyncReques t('POST', "testPost.p hp",
responseHandler , jstr);
24 };
-----------------------------------------------------------------------------------------------------------------

* line 17 makes the object
* line 22 converts to a json string
* line 23 issues the ajax request


As a comparison, this is how I deal with the request in Perl:

-----------------------------------------------------------------------------------------------------------------
1 #!/usr/bin/perl
2
3 use strict;
4 use JSON;
5
6
7 my $jsonObj = new JSON;
8 my $buffer;
9 my $param;
10 my $jsonStr = {};
11
12 print "Content-type: text/html\n\n";
13
14 read (STDIN, $buffer, $ENV{'CONTENT_L ENGTH'});
15
16 if (defined $buffer && $buffer ne "") {
17 $param = $jsonObj->jsonToObj($buf fer);
18 }
-----------------------------------------------------------------------------------------------------------------

* line 14 does a read from STDIN
* line 17 converts the JSON string to a hash in Perl
1. how do I do something similar in PHP?
2. I found that can get the CONTENT_LENGTH from $_SERVER
3. I found that $_POST doesn't give me anything valid - as expected.
4. Doing a "$myInput = fopen('php://stdin', 'r')" doesn't help
either

Can someone please point me in the right direction to recover the
passed in JSON string from the client?

thanks in advance.
-S

Aug 26 '07 #1
1 2279
On Aug 26, 11:18 am, seth <satyen.sh...@g mail.comwrote:
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
server side.

Here is the Javascript which show: [this is a code snippet only]
1. creating an object
2. encoding to JSON string
3. making the ajax request.

-----------------------------------------------------------------------------------------------------------------
12 function makeRequest() {
13 var jsonObj;
14 var transaction;
15 var jstr;
16
17 jsonObj = {
18 action : "ack",
19 var1 : "fly"
20 };
21
22 jstr = jsonObj.toJSONS tring();
23 transaction =
YAHOO.util.Conn ect.asyncReques t('POST', "testPost.p hp",
responseHandler , jstr);
24 };
-----------------------------------------------------------------------------------------------------------------

* line 17 makes the object
* line 22 converts to a json string
* line 23 issues the ajax request

As a comparison, this is how I deal with the request in Perl:

-----------------------------------------------------------------------------------------------------------------
1 #!/usr/bin/perl
2
3 use strict;
4 use JSON;
5
6
7 my $jsonObj = new JSON;
8 my $buffer;
9 my $param;
10 my $jsonStr = {};
11
12 print "Content-type: text/html\n\n";
13
14 read (STDIN, $buffer, $ENV{'CONTENT_L ENGTH'});
15
16 if (defined $buffer && $buffer ne "") {
17 $param = $jsonObj->jsonToObj($buf fer);
18 }
-----------------------------------------------------------------------------------------------------------------

* line 14 does a read from STDIN
* line 17 converts the JSON string to a hash in Perl

1. how do I do something similar in PHP?
2. I found that can get the CONTENT_LENGTH from $_SERVER
3. I found that $_POST doesn't give me anything valid - as expected.
4. Doing a "$myInput = fopen('php://stdin', 'r')" doesn't help
either

Can someone please point me in the right direction to recover the
passed in JSON string from the client?

thanks in advance.
-S
I found what I needed. Maybe it will help others?

Idea was to find a way to handle RAW data in PHP.

There are 2 ways:

1. enable $HTTP_RAW_POST_ DATA
2. use PHP input/output streams - 'php://input'

It's cited here:
http://www.php.net/manual/en/wrappers.php.php

Here is my sample code which works great!

1 <?php
2
3 header('Content-type: text/html');
4 $data = file_get_conten ts('php://input', 'r');
5 print ($data);
6 ?>
7

Here is the output:
=============== =============== =

Send Ajax Request
Got Data! ...
Transaction id: 12
HTTP status: 200
Status code message: OK
# HTTP headers:

Date: Sun, 26 Aug 2007 21:11:08 GMT Server: Apache X-Powered-By:
PHP/5.2.1 Content-Length: 30 Keep-Alive: timeout=15, max=99
Connection: Keep-Alive Content-Type: text/html; charset=UTF-8
PHP response: {"action":"ack" ,"var1":"fly "} <<<<=========== ==
[recovered JSON string]
Argument object: undefined
Aug 26 '07 #2

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

Similar topics

16
2671
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post he calls what I am suggesting "brittle", his own Javascript JSON parser is not valid JSON, but rather conforms to my proposed variation on JSON!!...
20
6815
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object...
54
8241
by: VK | last post by:
Mission statement: A mechanics to get text stream into browser from any Web location without reloading the current page. 1) This mechanics has to support *at the very least* IE 5.5 and higher and Firefox 1.5 and higher: but it may be completely different from one browser to another. It is important only to be able to build an uniformed...
8
19567
by: MD | last post by:
I want to put the xml document into the JSON like the following. However, usually my xml document has new line for the xml document. Is there any option to skip the new line in the JSON or I just have to remove them manually . MD
4
13084
by: =?Utf-8?B?UmFqaXYgVmVybWE=?= | last post by:
Hi, I am using .NET 3.5 JSON deserializer. When I am passing my JSON server response to DataContractJsonSerializer, DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Envelope)); It is throwing an exceptions "Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''. error?"
6
2811
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
1
13288
by: HarishAdea | last post by:
Hi, I am trying to run the JAVA pgm, but it is giving error as "selection does not contain a main type". The filename is "ScoreLeadSummary.java" when i try to run it or debug,it gives the pop up as "selection does not contain a main type" Please help me out in resolving the above issue to run the .java file. I am facing same issue with...
9
2213
by: BryanA | last post by:
Is it possible to parse JSON data and then post it to a mysql db with the data in their respective fields?
2
2654
by: xdevel1999 | last post by:
Hi, I have the following problem: when I try to access to the array in Big object I have 'Big is not defined' but when I try to access to the function in Low object I have no error!!!! WHY?? Big = {
0
7496
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...
0
7685
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. ...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6014
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...
1
5354
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...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.