开发者

Setting the HTTP Accept header for JsonRestStore

开发者 https://www.devze.com 2023-02-10 18:37 出处:网络
I\'m using JsonRestStore but would like to add a custom Accept header to it.What\'s the best way to go about this?

I'm using JsonRestStore but would like to add a custom Accept header to it. What's the best way to go about this? This is similar to how the dijit.layout.ContentPane allows you to affect the underlying XHR开发者_运维百科 by setting ioArgs. So the question could be "what is JsonRestStore's ioArgs?"

I'm using declarative syntax, but would gladly like to see both methods...

(Please note: I'm not interested in hacking my way around this by modifying the base XHR.)


Your best bet is providing a custom service to JsonRestStore. The easiest way I found of doing this is building the service from dojox.rpc.Rest. In the constructor you can provide a function to create the request arguments for all XHR requests. E.g.

function getRequest(id, args) {
    return {
        url: '/service/' + id,
        handleAs: 'json',
        sync: false,
        headers: {
            Accept: 'your custom header'
        }
    }
}

var service = new dojo.rpc.Rest('/service/', true /*isJson*/, 
                                undefined /*schema*/, getRequest);
var store = new dojox.data.JsonRestStore({ service: service });

This completely ignores the args parameter that can include sorting and range arguments to your service.

These links will provide more information:

  • Use Dojo's JsonRestStore with your REST services: IBM developerWorks article with a more advanced and customizable solution
  • RESTful JSON + Dojo Data: Sitepen post
  • dojox.rpc.Rest source file (look for service._getRequest)
0

精彩评论

暂无评论...
验证码 换一张
取 消