Custom HTTP Headers with JsonRest Store (dojo)

I was wondering if there is a way to set my own custom HTTP headers in a Get ajax request (xhr.get), which automatically creates a JsonRest repository.

There is a related topic, but without a great solution: Dojo Data Grid with custom HTTP headers

I saw the JsonRest implementation in dojo.store.JsonRest ', including the constructor, and this is not obvious if we can do this or not (but I don't think so). An example of a used JsonRest repository:

var store = new JsonRestStore({target: "/Table/" }); 
+4
source share
1 answer

I would subclass dojo.store.JsonRest as you can see in this jsFiddle .

a. Subclass dojo.store.JsonRest :

 var MyJsonRest = declare(JsonRest, { get: function(id, options) { return this.inherited( arguments, [id, lang.mixin(this.defaultGetHeaders, options)] ); } }); 

So, you override the get method that calls the superclass' get , but the second options argument (i.e. the headers) will now also contain the properties from this.defaultGetHeaders .

B. Define defaultGetHeaders in the constructor:

 var myJsonRest = MyJsonRest({ target: "/echo/json/", defaultGetHeaders: { userId: "xyz", requestedBy: "abc", requestedFrom: "123" } }); 

C. By calling the myJsonRest.get() method, you can also overwrite the default headers:

 myJsonRest.get("someId", { requestedFrom: "321"}).then(function(result) { console.log(result); }); 

E. Check the request headers:

enter image description here

+7
source

Source: https://habr.com/ru/post/1403047/


All Articles