views:

454

answers:

1

I'm trying to populate a Dojo grid with data from an ASP.Net web service. There is going to be a lot of rows, so I want to implement paging on the server side, so the web service will accept parameters "start" and "count". I've gotten pretty close, by using QueryReadStore and overriding the "fetch" function to add additional parameters (the web service requires more than just start and count).

The immediate problem I've encountered is that the web service is returning the data as XML. I believe this is because the request does not contain a Content-Type header indicating a preference for json (which the grid wants). I am using an Http-Post rather than the default Get. Is a ResponseFormat attribute supposed to override this? It doesn't work for me.

So, how do I get the data as json? Or alternately, am I barking up the wrong tree? It would seem like a pretty common thing to glue together a grid and a web service. Does Dojo have any built-in functionality for this that I am just not aware of?

Thanks

+1  A: 

You should be able to simply set the handleAs parameter on your call to dojo.xhrPost(..) to "xml", this will bind the XML to javascript objects to make your life easier while handling the data:

dojo.xhrPost({
    url: "http://whatever.com/someendpoint",
    handleAs: "xml",
    load: function(response, ioArgs){
        /*
         * Do something with response, it's a JS object that reflects the XML.
         */
    }
});

Alternatively, you could chose to send different HTTP Headers in your AJAX call by using the headers property of the argument object for dojo.xhrPost(..):

dojo.xhrPost({
    url: "http://whatever.com/someendpoint",
    handleAs: "json",
    headers: {
        "Accept" : "text/javascript, text/json, application/json, application/javascript"
    },
    load: function(response, ioArgs){
        /*
         * Do something with response, it's a JS object that reflects the JSON.
         */
    }
});
JasonWyatt
That looks like it will allow dojo to handle xml return data, but how does this fit in with the dojo grid's automatic paging? For context, I started with the example at the end of http://www.linuxjournal.com/article/10380. It seems like I should avoid making explicit calls to the web service, and just manipulate the request object. Adding headers to it like you suggest doesn't seem to do it, though I might not be doing that correctly.
Brian
well the calls to dojo.xhrPost would be requesting each individual page of the data... so they would automatically return the right page inside of your fetch function. If you want to grab all of your data (all pages) in one AJAX request, you'll run the risk of defeating the purpose of paging (by having to wait for a huge response to come back)
JasonWyatt
Adding that accept header didn't do the trick. Firebug shows that it is sent as part of the request, but the data still comes back as xml. handleAs didn't do anything either, as far as I can tell. The response object just comes back as the Xml DOM, not as any sort of collection the grid is able to use. Also, from what I can tell, I'm better off not calling xhrPost from my fetch override. http://docs.dojocampus.org/dojox/data/QueryReadStore makes it sound like I should call the base version when I'm done, which when I look at it adds the success and fail handlers.
Brian