Sending JSON Data to a server

View: New views
6 Messages — Rating Filter:   Alert me  

Sending JSON Data to a server

by Jochen Luig-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've recently started to look into Dojo. On the server side I have an
App that automatically deserializes Data provided by the client and
serializes it before sending a response. My format of choice is JSON.
Alas, the dojo.xhrPost seems to insist on sending the data as key=value
pairs separated by a '&' which on one hand lead my App to believe there
wasn't any POST data (as it expects JSON and thus finds nothing to
serialize). On the other hand I can't comfortably send deeply nested
data to the server. By chance I came across dojo.rawXhrPost which does
the trick if used in this way (I couldn't get dojo.xhrPost to behave in
a similar way):

dojo.rawXhrPost({
    url: "http://path.to/whatever",
    handleAs: "json",
    postData: json_string,
    headers: {"Content-Type": "application/json"},
    handle: function (data, args) {
        if (typeof(data) == "error") {
            // error handling
        } else {
            // do something
        }
    }
});

Although this works quite well for me so far, I have a few questions:

1.) dojo.rawXhrPost isn't documented exhaustively. E.g. the (quite
recent) Addison Wesley Dojo Book
(http://www.amazon.com/Dojo-JavaScript-Library-Applications-Developers/dp/0132358042/ref=pd_bbs_sr_3?ie=UTF8&s=books&qid=1218108346&sr=8-3 ) doesn't mention the function at all. Is it likely to be deprecated anytime soon (or is it already)? If so, what would be a reasonable alternative?
2.) having to override the Content-Type header by hand seems quite
"hackish" to me. Is there another way?
3.) the conditional in the handler function (I found that one in a
tutorial) always gets into the "else" part even if it gets back a 405
response code from the server. Is that expected behaviour? I would have
expected anything in the 400-range to result in an error. How can I do
different things according to specific http response codes?

Thanks

Jochen

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://dojotoolkit.org/docs/book
Forums: http://dojotoolkit.org/forum
Dojo-interest@...
http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

Re: Sending JSON Data to a server

by Anthony Fryer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
I send json to the server using xhrPost by assigning the json a parameter. 
 
ie.
 
dojo.xhrPost({
  url: './blah',
  content: { json: dojo.toJson(myJsonObject) }
});
 
On the server i just get the "json" parameter value and deserialize it.

> From: jochen.luig@...
> To: dojo-interest@...
> Date: Thu, 7 Aug 2008 13:38:50 +0200
> Subject: [Dojo-interest] Sending JSON Data to a server
>
> Hi,
>
> I've recently started to look into Dojo. On the server side I have an
> App that automatically deserializes Data provided by the client and
> serializes it before sending a response. My format of choice is JSON.
> Alas, the dojo.xhrPost seems to insist on sending the data as key=value
> pairs separated by a '&' which on one hand lead my App to believe there
> wasn't any POST data (as it expects JSON and thus finds nothing to
> serialize). On the other hand I can't comfortably send deeply nested
> data to the server. By chance I came across dojo.rawXhrPost which does
> the trick if used in this way (I couldn't get dojo.xhrPost to behave in
> a similar way):
>
> dojo.rawXhrPost({
> url: "http://path.to/whatever",
> handleAs: "json",
> postData: json_string,
> headers: {"Content-Type": "application/json"},
> handle: function (data, args) {
> if (typeof(data) == "error") {
> // error handling
> } else {
> // do something
> }
> }
> });
>
> Although this works quite well for me so far, I have a few questions:
>
> 1.) dojo.rawXhrPost isn't documented exhaustively. E.g. the (quite
> recent) Addison Wesley Dojo Book
> (http://www.amazon.com/Dojo-JavaScript-Library-Applications-Developers/dp/0132358042/ref=pd_bbs_sr_3?ie=UTF8&s=books&qid=1218108346&sr=8-3 ) doesn't mention the function at all. Is it likely to be deprecated anytime soon (or is it already)? If so, what would be a reasonable alternative?
> 2.) having to override the Content-Type header by hand seems quite
> "hackish" to me. Is there another way?
> 3.) the conditional in the handler function (I found that one in a
> tutorial) always gets into the "else" part even if it gets back a 405
> response code from the server. Is that expected behaviour? I would have
> expected anything in the 400-range to result in an error. How can I do
> different things according to specific http response codes?
>
> Thanks
>
> Jochen
>
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://dojotoolkit.org/docs/book
> Forums: http://dojotoolkit.org/forum
> Dojo-interest@...
> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest



to the movies Shout your Messenger buddies
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://dojotoolkit.org/docs/book
Forums: http://dojotoolkit.org/forum
Dojo-interest@...
http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

Re: Sending JSON Data to a server

by Jochen Luig-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Am Donnerstag, den 07.08.2008, 12:11 +0000 schrieb Anthony Fryer:

> I send json to the server using xhrPost by assigning the json a
> parameter.
>  
> ie.
>  
> dojo.xhrPost({
>   url: './blah',
>   content: { json: dojo.toJson(myJsonObject) }
> });
>  
> On the server i just get the "json" parameter value and deserialize
> it.

Yes, I read about that approach in the forums. The problem with this is
that my server will just take the HTTP-Request's body and try to
serialize it. Thus, your way will leave me having to deserialize the
data myself (as long as I don't want to introduce some ugly hack into my
App).

Bye,

Jochen

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://dojotoolkit.org/docs/book
Forums: http://dojotoolkit.org/forum
Dojo-interest@...
http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

Re: Sending JSON Data to a server

by Maulin Shah, MD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

dojox.rpc.Rest uses rawXhr, and is becoming very popular, so I really  
don't think it will be deprecated.

On Aug 7, 2008, at 5:27 AM, Jochen Luig wrote:

> Hi,
>
> Am Donnerstag, den 07.08.2008, 12:11 +0000 schrieb Anthony Fryer:
>> I send json to the server using xhrPost by assigning the json a
>> parameter.
>>
>> ie.
>>
>> dojo.xhrPost({
>>  url: './blah',
>>  content: { json: dojo.toJson(myJsonObject) }
>> });
>>
>> On the server i just get the "json" parameter value and deserialize
>> it.
>
> Yes, I read about that approach in the forums. The problem with this  
> is
> that my server will just take the HTTP-Request's body and try to
> serialize it. Thus, your way will leave me having to deserialize the
> data myself (as long as I don't want to introduce some ugly hack  
> into my
> App).
>
> Bye,
>
> Jochen
>
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://dojotoolkit.org/docs/book
> Forums: http://dojotoolkit.org/forum
> Dojo-interest@...
> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://dojotoolkit.org/docs/book
Forums: http://dojotoolkit.org/forum
Dojo-interest@...
http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

Re: Sending JSON Data to a server

by James Burke-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Aug 7, 2008 at 4:38 AM, Jochen Luig <jochen.luig@...> wrote:
> 1.) dojo.rawXhrPost isn't documented exhaustively. E.g. the (quite
> recent) Addison Wesley Dojo Book
> (http://www.amazon.com/Dojo-JavaScript-Library-Applications-Developers/dp/0132358042/ref=pd_bbs_sr_3?ie=UTF8&s=books&qid=1218108346&sr=8-3 ) doesn't mention the function at all. Is it likely to be deprecated anytime soon (or is it already)? If so, what would be a reasonable alternative?

Since it is a public API, it will be supported for all Dojo 1.x
releases. You are safe to continue using it.

> 2.) having to override the Content-Type header by hand seems quite
> "hackish" to me. Is there another way?

When you do a raw post, you are deciding to handle encoding of the
request body yourself. Since Dojo cannot guess what kind of encoding
you will do, then you need to specifically tell it. Also, it is not
uncommon with other dojo.rawXhrPost calls to want to set other HTTP
headers. The headers object seems like a nice way to allow you to
specify all the headers you want.

> 3.) the conditional in the handler function (I found that one in a
> tutorial) always gets into the "else" part even if it gets back a 405
> response code from the server. Is that expected behaviour? I would have
> expected anything in the 400-range to result in an error. How can I do
> different things according to specific http response codes?

If you have:
handle: function (data, args) {}

then args.xhr will be the XMLHttpRequest object. You can do a test for
args.xhr.status to get the HTTP status code and branch your code
accordingly.

James
_______________________________________________
FAQ: http://dojotoolkit.org/support/faq
Book: http://dojotoolkit.org/docs/book
Forums: http://dojotoolkit.org/forum
Dojo-interest@...
http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

Re: Sending JSON Data to a server

by TechDisc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am new to Dojo.
Do you have any example that will send JSON data to server and receive the JSON data from server using XHRPost, then populate into grid ?

Thanks,
Techdisc