473,378 Members | 1,175 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,378 software developers and data experts.

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.toJSONString();
23 transaction =
YAHOO.util.Connect.asyncRequest('POST', "testPost.php",
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_LENGTH'});
15
16 if (defined $buffer && $buffer ne "") {
17 $param = $jsonObj->jsonToObj($buffer);
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 2260
On Aug 26, 11:18 am, seth <satyen.sh...@gmail.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.toJSONString();
23 transaction =
YAHOO.util.Connect.asyncRequest('POST', "testPost.php",
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_LENGTH'});
15
16 if (defined $buffer && $buffer ne "") {
17 $param = $jsonObj->jsonToObj($buffer);
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_contents('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
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...
20
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... ...
54
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...
8
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...
4
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...
6
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
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...
9
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
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?? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.