On Nov 11, 2010, at 8:20 AM, fashionpeople wrote:

Hi T.J. Crowder,

var jsonRequest         = JSON.stringify(requestObject);

new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', {
method:     'POST',
requestHeaders: {Accept: 'application/json'},
parameters: {json: jsonRequest},
// ...

In PHP:

$request           = $this->getRequest();
$json                = $request->getParam("json");

// HERE decode fails with data sended from chrome
$requestObject = $zendJson->decode($json, Zend_Json::TYPE_OBJECT);

With Chrome decoding fails too. Why? :\


Can you print out the raw value of your $json string after the getParam call? Then echo a few paragraph returns and also print the raw value of $_POST['json'].

You have two different "black boxes" here -- getParam() and $zendJson- >decode(). Either one of these may be "helping" you by decoding some part of the variable posted by Prototype. You may be double-decoding something, or missing the decode on something else. Try printing the raw text to the screen each time and see what and where the value changes in your server-side.

It's unlikely that encodeURIComponent is different between two browsers. There may indeed be some other part of Prototype that is over-correcting some encoding on the browser side, based on a mis- reading of the user agent or some other variable.

But I would button down the server side first, and make sure that you are getting the very same raw data in and that your conversions are proceeding in a normal fashion within the PHP domain.

If two browsers send two different raw POST bodies, then you're right -- the issue is within Prototype, or maybe within the browser and Prototype isn't spackling over it the way it usually does.

You may also have found a difference of opinion between PHP JSON and JS JSON -- these do exist and they are very annoying and require hunt- and-peck application of addslashes() and stripslashes(). Which reminds me -- do look at the current value of magic_quotes_gpc, both on your server and within your application. That's another snake-pit I have had to cross with JSON on PHP.

Walter


On 11 Nov, 03:47, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
Hi,

It's T.J., not J.

i want convert requestObject in JSON string and send to the server.

 var jsonRequest         =
encodeURIComponent(JSON.stringify(requestObject));
new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', {
            method:     'POST',
            requestHeaders: {Accept: 'application/json'},
            parameters:  jsonRequest,
            //...

Let's look at the code I gave you before AGAIN:

    var jsonRequest = ...;
    new Ajax.Request( // ...
        parameters: {json: jsonRequest}
        // ...
    });

As you can see, I used jsonRequest. I did that for a reason, so it
would fit in with your original code, which did this:

var jsonRequest         = JSON.stringify(requestObject);

So:

function sendMessage(baseUrl, idNickRcv, msg) {
        var requestObject       = new Object();
        requestObject.idNickRcv = idNickRcv;
        requestObject.msg       = msg;

        var jsonRequest         = JSON.stringify(requestObject);

        if ((idNickRcv) && (msg)) {
new Ajax.Request(baseUrl + '/usermsg/index/ sendmessage', {
                        method:     'POST',
                        requestHeaders: {Accept: 'application/json'},
parameters: {json: jsonRequest}, // <=== CHANGE IS HERE
                        onSuccess:  function(transport, json) {
                                //use and handle foo response data
                        }
                        ,
                        on500:      function(transport) {
                                //handle error, inform user
                        },
                        onComplete: parseSendMessage
                });
        }

    // ...your code left off here

That tells Prototype to do any URI-encoding necessary for you (you
still have to stringify the object first, which is why I used
jsonRequest, not requestObject). It sends a nice, normal URL-encoded
request (technically an "application/x-www-form-urlencoded" POST) to
the server with a single form field, 'json', whose value is the URL-
encoded jsonRequest string.

Having done that, you ALSO have to change the server side to retrieve
and un-JSON that form field:

   $request = $_POST["json"];
$requestObject = Zend_Json::decode($request); // Or your $zendJson, whatever that is

Now again, this is not the only way to do it. It's _possible_ to send
pure JSON without doing the form wrapper around it as I've done above
(in which case you probably wouldn't URL-encode at all, because you
wouldn't be sending URL-encoded data). You can send any content type
you like with a POST. If you're not sending data in the "application/x-
www-form-urlencoded" encoding, you have to specify what you're doing
by setting the "Content-Type" header (for pure JSON, it would be
"application/json"). You have to handle issues like ensuring there's a
carriage return every couple of hundred characters and such (because
of links in the chain that may handle HTTP poorly).

But whenever possible, I stick to sending standard forms, because
they're convenient and reliable across browsers and servers. Forms
(that is, POST requests with the content type "application/x-www- form-
urlencoded") are well-tested, and well-supported by frameworks. Since
you appear to be in control of both the client and server sides of
what you're building, I'd probably stick with sending a form
containing a field with the JSON in it, rather than going through the
hassle of sending JSON without a form wrapper around it.

Hope this helps,

-- T.J.

On Nov 11, 12:49 am, fashionpeople <fashionpeople.busin...@gmail.com>
wrote:







J. Crowder do not get me wrong, I thank you for the effort they put in
helping others.

i readed about your suggest to send data in this simple way:

    var jsonRequest = ...;
    new Ajax.Request( // ...
        parameters: {json: jsonRequest}
        // ...
    });

but my question is, i have all based application such statements:

      var requestObject           = new Object();
      requestObject.idNickRcv = idNickRcv;
      requestObject.msg          = msg;

i want convert requestObject in JSON string and send to the server.

 var jsonRequest         =
encodeURIComponent(JSON.stringify(requestObject));
new Ajax.Request(baseUrl + '/usermsg/index/sendmessage', {
            method:     'POST',
            requestHeaders: {Accept: 'application/json'},
            parameters:  jsonRequest,
            //...

In this way works only IE and FireWorks. Not on Chrome always decoding
fails.
you must use stringify? scriptaculous encodes them alone?

Thank you

--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com . To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en .


--
You received this message because you are subscribed to the Google Groups "Prototype 
& script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to