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? :\


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-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