'How to compress web service response json in ajax call
I am using asmx web service and calling it through ajax as
$.ajax({ url: 'xxxxxxx.asmx/Getxxxxxxxx', method: 'post', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (data) { $(data.d).each(function (index, category) {
My service returns a list of objects. I was reading on how to compress the response of a web service and it seems to me that to do this I need to override the GetWebRequest method and use Gzip/Deflate (in a class derived from web service proxy class).
The next step would be to use an instance of this class (which overrides GetWebRequest) and use this in application.
Since, I am using ajax call, I am not sure how to use this instance of class which overrides GetWebRequest method.
Thank you.
Solution 1:[1]
In order to send the compressed response to client, from your web-service, you must be using content-encoding field and set its value to compress.
content-encoding: gzip
But, before that you must check the value of "Accept-Encoding" header send by client. The possible values for this header is
Accept-Encoding: compress, gzip
Accept-Encoding:
Accept-Encoding: *
Accept-Encoding: compress;q=0.5, gzip;q=1.0
Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
The above mentioned guidelines are applicable, irrespective of whatever language/technology you are using.
For more information of HTTP protocol headers, refer to
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
EDIT
"Accept-Encoding" request header must be set in your ajax request. You can do that, by setting "headers" in your ajax request as follow :
$.ajax({
url: 'xxxxxxx.asmx/Getxxxxxxxx',
method: 'post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
headers: {
"My-First-Header":"first value",
"My-Second-Header":"second value"
}
success: function (data) {}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |