You are here: How does one handle JSON strings from the browser « PHP Programming Language « IT news, forums, messages
How does one handle JSON strings from the browser

Posted by seth on 08/26/07 18:18

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация