[Dojo-interest] Sending JSON Data to a server

Jochen Luig jochen.luig at skytel.de
Thu Aug 7 11:38:50 UTC 2008


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



More information about the Dojo-interest mailing list