Try something like this.
<?php
$a_SomeNestedData = array
(
// Some manual data.
'Key1' => 'Data1',
'Key2' => array
(
'Key2.1' => 'Data2',
'Key2.2' => 'Data3',
),
// Some external data.
'POST' => array_key_exists('_POST',$GLOBALS) ? $_POST : NULL,
'GET' => array_key_exists('_GET',$GLOBALS) ? $_GET : NULL,
'COOKIE' => array_key_exists('_COOKIE',$GLOBALS) ? $_COOKIE : NULL,
);
$s_Response = rawurlencode(json_encode($a_SomeNestedData));
echo $s_Response;
?>
On the CLI for me, this produces ...
%7B%22Key1%22%3A%22Data1%22%2C%22Key2%22%3A%7B%22Key2.1%22%3A%22Data2%22%2C%22Key2.2%22%3A%22Data3%22%7D%2C%22POST%22%3A%5B%5D%2C%22GET%22%3A%5B%5D%2C%22COOKIE%22%3A%5B%5D%7D
Now, assuming this was the response to an AJAX request, you would then use ...
var obj_SomeNestedData = unescape(transport.responseText)
in your onSuccess handler to get the data back again.
xpmstos wrote:
> I use a generic PHP server which uses the PECL ext/json.
> My server use this to get the data:
> $data = json_decode(file_get_contents('php://input'));
>
> The client-code is simpel, i don't select any encoding-formats etc.:
> new Ajax.Request('server.php',
> {
> method: 'post',
> parameters: JSON.stringify({
> method: 'foo::bar',
> params: [data],
> id: new Date().getTime()
> }), ...
Hello XP,
I'm not a network guy, but I imagine you may find that some users
(i.e. those using proxy servers, or those using certain browsers) may
encounter problems when attempting to post non-url encoded characters.
In fact, it may not work for anybody. Traditionally, setting up a
scheme like this in php would simply request php to build the data
from the posted string:
<php>
$data = $_POST;
// where data then looks like the following, based on an ajax-posted
object run through Hash.toQueryString()
$data = array(
'method'=>'foo::bar',
'params'=>array('some data',array('some nested'=>'data')),
'id'=>'1123123123'
);
</php>
If you WOULD like to try sending JSON, you could override the original
prototype Hash.toQueryString method by defining it after inclusion of
prototype something like so:
Object.extend(Hash, {
toQueryString: function(obj) {
JSON.stringify(obj);
}
});
then, in the parameters, send a simple object like so:
new Ajax.Request('server.php',
{
method: 'post',
parameters: {
method: 'foo::bar',
params: data,
id: new Date().getTime()
}), ...
If you do try it, let us know what you find.
Regards,
Ken Snyder
- Show quoted text -
On 04/03/07, xpmstos <[EMAIL PROTECTED]> wrote:
>
> Hello Christophe, Hello Ken,
>
> i use atm FF2.0, but i want a compatibility for I.E.also etc.
> When i use your simple object:
> > new Ajax.Request('server.php',
> > {
> > method: 'post',
> > parameters: {
> > method: 'foo::bar',
> > params: data,
> > id: new Date().getTime()
> > }), ...
> the first chars in my string would be correct, but not the followings,
> the result look like this:
> {"method%22%3A%22foo%3A%3Abar%22%2C%22params%22%3A%
>
> I worked with another solution now. I use my javascript code like
> before:
> new Ajax.Request('server.php',
> {
> method: 'post',
> parameters: JSON.stringify({
> method: 'foo::bar',
> params: [data],
> id: new Date().getTime()
> }),
> onSuccess:doSomething,
> onFailure: function(){ alert('Something went wrong...') }
> });
>
> But new is my little solution on the server side, i will tell, it's
> simple but it will works:
> function hex2str($hex) {
> $str = ''; $loop = 1;
> for($i=0; $i<strlen($hex); $i+=$loop) {
> $chr = substr($hex,$i,1);
> if($chr=='%') { // found next hex
> $chr = substr($hex,$i+1,2);
> $str .= chr(hexdec($chr));
> $loop = 3;
> }
> else { // next char
> $str .= $chr;
> $loop = 1;
> }
> }
> return substr($str,0,strlen($str)-1); // kill the last "=", little
> bug
> }
>
> And now, i receive the string, decode it firstly with hex2str, before
> it decode as a JSON objekt:
> $string = hex2str(file_get_contents('php://input'));
> $objJSON=json_decode($string);
>
> What would you think, is it a terrible solution? Or is it acceptable?
> Thanks for you help Christophe and Ken
>
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Spinoffs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to [EMAIL PROTECTED]
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-spinoffs?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---