Setting additional headers while making Ajax request

In the How to prevent caching of Ajax request using Dojo i talked about how you can use preventCache flag to change the URL of Ajax request so that browser and caching proxy is not able to cache it.

But what if you want the response to be cached but validated, or you want to set Cache-Control: max-age=0 header so that the request is validated end to end, similar to what happens when you click on browser refresh button.

The dojo toolkit provides a way to set headers while making xhrGet() call like this

dojo.xhrGet({
url: "<portlet:resourceURL/>",
headers: {
"Cache-control" :"max-age=0",
"PreventCache": "nocache"
},

load: function(data, ioargs){
dojo.byId("resourceResponse").innerHTML = data;
},
error: function(error,ioargs){
alert(error);
}
});


You can set headers by adding header field with map of name and values while making xhrGet() call. In my case i am setting Cache-Control and PreventCache header. and this is what happens when i make the request. If the resource is cached by browser the browser will not make a conditional get request to verify if it is changed. But it will add the headers like this

No comments: