Login Register

JSON support isn't good enough

We use dojo as main platform on WEB.
In my opinion, the browser side can send data to server side by XHR. It makes Browser/Server and Client/Server are very much alike. Similarly, I want to define a uniformed interface by JSON object.
But dojotoolkit stopped me, dojo.io.bind can't send a JSON string to server side.
I can use a trick like "/servlet?jsonstring=blablabla" to send json string, but it smells bad.
Could someone help me ? or Should we change the dojo's API ?

You can send text... JSON is

You can send text... JSON is serialized Javascript objects in a text string... How can you not send JSON then?
-Karl

Maybe we can use another

Maybe we can use another property to support json object, How about this one :
var data = {name: "mike", nickname: "michael"};
dojo.io.bind({
url: "service/userservice/addUser",
method: "POST",
load: function(type, data, evt){
//......
},
json: data
});

Unnecessary, see the

Unnecessary, see the postcontent property or take a look at the dojo.rpc.JsonService which does this same thing.

I don't think it is a good

I don't think it is a good idea, json is a simple and right way to transfer data between server and client/browser, that is a pure technology which isn't depend on other language's API. Use it as a RPC language like java(rmi) , corba will make the application far away from the REST's principle.
We need json, not jsonrpc.

I have no idea what you are

I have no idea what you are talkign about. I'm very familiar with JSON, JSON-RPC, and Dojo, and I can say with 100% certainty we have full support for JSON and you can post JSON wehrever you want very easily. I've described out how post JSON to the server. I said you could look at the JSON-RPC as an example as it does the exact same thing you are askign to do. if you would read the code that i posted, you will see that it is posting a json string.

I think this one is the best

I think this one is the best solution -- http://dojotoolkit.org/node/535#comment-1076
Thanks a lot.

yeah, thats why i posted it.

yeah, thats why i posted it.

It most certainly can send

It most certainly can send JSON strings to the server; you'll send it either as a GET (i.e. on the query string) or as a POST. Either way, what you should probably do is set the content object of bind to your string:
var myJsonString = dojo.json.serialize(myObjects);
dojo.io.bind({
    url:"someUrl.php",
    content:{
        json:myJsonString
    }
});

that's not good enough, if

that's not good enough, if you can send json string directly, why use "json" prefix ?
On the other hand, character encoding also take some trouble on it, I read input stream from servlet, but what I can get is "json=%7B%22name%22%3A%22a%22%2C%22description%22%3A%22a%22%2C%22ok%22
%3A%22OK%22%2C%22cancel%22%3A%22Cancel%22%7D".
js code:

var data = '{"name":"a","description":"a","ok":"OK","cancel":"Cancel"}';
dojo.io.bind({
    url: "service/userservice/addUser",
    method: "POST",
    load: function(type, data, evt){
        if(type=="load"){
            var result = eval("(" + data + ")");
            store.addData(result,result.id);
            var addDialog = dojo.widget.byId("addDialog").hide();
        }else{
            alert(data);
        }
    },
    content: {json: data.toJSONString()}
});

It likes a bad idea.

well in this case you

well in this case you wouldnt' do that..you'd do do content: data

That will transform your javascript object into post parameters. I think what you are asking for his how to do a raw post of json content. You would do that like this:

dojo.io.bind({
   url: "someurl",
   method: "POST",
   postContent: dojo.json.serialize(someObject),
   load: function(type,data) { /* some load thing */ },
   error: function(type, data) { /* some error thing */}
});

Great!! That's what I want.

Great!! That's what I want.

The "json" prefix was just

The "json" prefix was just an example of a variable name; it doesn't need to be "json", it can be anything you want--and as dmachi mentioned, you can post JSON raw if you want.

A question.

[EDIT: Fixed this post so that it is readable, with a real subject.]

Why all of you use "dojo.io.bind" instead of "dojo.xhrGet" ? "dojo.xhrGet" is made just for Ajax , it is designed to simplify our codes.
But ,well ,I find the same problem , it can only send string type , it can't send object type. I want get a json object from the server by "xhrGet" , what shall I do?

<script type="dojo/method" event="onClick" args="item">
                        var ID = classesStore.getValue(item,"RealID");
                        dojo.xhrGet({
                                url: '../JSON/getSingleSmallClass.json.php',
                                load: myAjax,
                                error: myError,
                                content: {'ID': ID}
                        });
                </script>

<script  type="text/javascript">
function myAjax(data,ioArgs){
        alert(data);
        alert(data.length);
        alert(data[0].ID);
}
function myError(data, ioArgs) {
        alert('Error when retrieving data from the server!');
}
</script>

This thread is back from the Dojo 0.4 days.

What you want to do with xhrGet is add the following named argument:

dojo.xhrGet({
    url: '../JSON/getSingleSmallClass.json.php',
    handleAs: "json",
    load: myAjax,
    error: myError,
    content: {'ID': ID}
});

Otherwise you will need to manually evaluate your returned data.