leora
leora

Reputation: 196439

Should I be using POST or GET when retrieving JSON data into jqGrid in my ASP.NET MVC application?

I am using jqgrid in my ASP.NET MVC application. Currently I have mTYpe: 'POST' like this:

jQuery("#myGrid").jqGrid({
    mtype: 'POST',
    toppager: true,
    footerrow: haveFooter,
    userDataOnFooter: haveFooter,

But I was reading this article, and I see this paragraph:

Browsers can cache images, JavaScript, CSS files on a user's hard drive, and it can also cache XML HTTP calls if the call is a HTTP GET. The cache is based on the URL. If it's the same URL, and it's cached on the computer, then the response is loaded from the cache, not from the server when it is requested again. Basically, the browser can cache any HTTP GET call and return cached data based on the URL. If you make an XML HTTP call as HTTP GET and the server returns some special header which informs the browser to cache the response, on future calls, the response will be immediately returned from the cache and thus saves the delay of network roundtrip and download time.

Given this is the case, should I switch my jqGrid mType all to use "GET" from "POST" for the mType? (It says XML (doesn't mention JSON). If the answer is yes, then actually what would be a situation why I would ever want to use POST for jqGrid mType as it seems to do the same thing without this caching benefit?

Upvotes: 2

Views: 2744

Answers (4)

Oleg
Oleg

Reputation: 221997

The problem which you describe could be in Internet Explorer, but it will be not exist in jqGrid if you use default options.

If you look at the full URL which will be used you will see parameters like

nd=1339350870256

It has the same meaning as cache: true of jQuery.ajax. jqGrid add the current timestemp to the URL to make it unique.

I personally like to use HTTP GET in jqGrid, but I don't like the usage of nd parameter. The reason I described in the old answer. It would be better to use prmNames: {nd:null} option of jqGrid which remove the usage of nd parameter in the URL. Instead of that one can control the caching on the server side. For example the setting of

Cache-Control: private, max-age=0

is my standard setting. To set the HTTP header you need just include the following line in the code of ASP.NET MVC action

HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan (0));

You can find more details in the answer.

It's important to understand, that the header Cache-Control: private, max-age=0 don't prevent the caching of data, but the data will be never used without re-validation on the server. Using other HTTP header option ETag you can make the revalidate really working. The main idea, that the value of ETag will be always changed on changing the data on the server. In the case if the previous data are already in the web browser cache the web browser automatically send If-None-Match part in the HTTP request with the value of ETag from the cached data. So if the server see that the data are not changed it can answer with HTTP response having 304 Not Modified status and empty body of the HTTP response. It allows the web browser to use local previously cached data.

In the answer and in this one you will find the code example how to use ETag approach.

Upvotes: 3

VJAI
VJAI

Reputation: 32758

You should not use GET for all the purposes. GET requests are supposed to use for getting data from the server not for saving or deleting operation. GET requests has some limitation since the data you are sending to the server or appended as query-strings you can't send very large data using GET requests. Also you should not use GET request to send sensitive information to the server. You should the POST request in all the other cases like adding, editing and deleting.

As far as I'm aware jqgrid appends a unique key in every GET request so you don't get any benefit from browser caching.

Upvotes: 2

senfo
senfo

Reputation: 29026

One way around the caching behavior is to make the GET unique each time the request is made. jQuery.ajax() does this with "cache: false" by appending a timestamp to the end of the request. You can replicate this behavior with something similar:

uri = uri + '?_=' + (new Date()).getTime(); // uri represents the URI to the endpoint

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

If the data that the server sends changes, then you should use POST to avoid getting cached data everytime you request it.

Upvotes: 2

Related Questions