In an Ajax XHR request, in HTTP_ACCEPT what is the meaning of q=0.01?

Asked

Viewed 164 times

2

I have a communication via Ajax (xhr), and in my HEADER the value of HTTP_ACCEPT is application/json, text/javascript, */*; q=0.01.

I understand all the above values, I would like to find something to explain the q=0.01 because I can’t figure out what that means or what that rule is. Thank you!

1 answer

3


The q= is the "weight" factor, or is used to indicate the priority for the server (back-end), it is important to note that this can be used in other headers besides the Accept as an example:

  • Accept-Charset
  • Accept-Language
  • Accept-Encoding
  • TE

The q= can be called Quality values or q-values or q-factors and are used to describe the priority order of values in a comma-separated list. The importance of a value is marked by the suffix ; q = immediately followed by a value between 0 and 1 included (0.0 to 1.0 for example), with up to three decimal digits, the highest value denoting the highest priority and when not present, the default value is 1. As described in https://developer.mozilla.org/en-US/docs/Glossary/Quality_values

Example:

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

It shall indicate the following order of priority:

Valor Priority
text/html and application/xhtml+xml 1.0
application/xml 0.9
*/* 0.8

So this the Accept indicates which content-type types the browser accepts and the q= will set the priority, in the above case the request expects states for the bakc-end that accepts text/html, application/xhtml+xml and application/xml, but prioritizes the first 2, if available.

Note that the */* probably states that if you do not have the top 3 in the priority order, then it will be accepted any type, of course it is possible to customize at the time of the request, an example in with XMLHttpRequest (ajax):

var oReq = new XMLHttpRequest();

//Defina como false
oReq.open("GET", "/url", true);

oReq.setRequestHeader('Accept', 'application/json');

...

//Espera completar a requisição, geralmente congela o browser
oReq.send(null);

In this example as I did not specify the q= then it will be equivalent to saying that q=1 for application/json, however this has to be solved in the back-end (read at the end where I wrote extra).


Accept-Language

An interesting example of use I believe for most is with the Accept-Language, because if the browser or a request defines something like:

Accept-Language: pt, en-gb;q=0.8, en;q=0.7

You can resolve on the server side to search for pt, if not find the priority would be the British English and if not yet available this language for the specific page will be displayed the english (en;q=0.7).


Extra

In PHP there is the function Locale::acceptFromHttp, but I believe that it is only possible to install via PECL or manually

Here in this Soen question you have examples in PHP and C# of how to get the priority language:

in Java probably did not create example because for Web in Java there are different technologies.

  • 1

    Show William, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.