Can charset parameter be used with application/json content type in http/1.1?

后端 未结 3 1601
耶瑟儿~
耶瑟儿~ 2020-12-03 05:58

For example, is it valid ajax request:

$.ajax({
    type: \"POST\",
    url: \"SomePage.aspx/GetSomeObjects\",         


        
3条回答
  •  再見小時候
    2020-12-03 06:09

    The recent json rfc 7159 says:

    Note: No "charset" parameter is defined for this registration.
    Adding one really has no effect on compliant recipients.

    i.e., charset must be ignored by compliant recipients.

    It is consistent with rfc 2045: "MIME implementations must ignore any parameters whose names they do not recognize." because rfc 7159 still specifies: "Required parameters: n/a; Optional parameters: n/a" for application/json mime media type (no parameters).

    The json text is no longer constrained to be an object or an array and the old section 3 that computes the character encoding based on the first two characters is gone in the new rfc. UTF-8, UTF-16, or UTF-32 are allowed but there is no way to specify the encoding (no BOM, UTF-8 is the default).

    Can charset parameter be used with application/json content type in http/1.1?

    There is no harm if charset="utf-8" is used -- utf-8 is the default encoding for json text but other values might be misleading because the value must be ignored by compliant recipients. It can only break clients that parse Content-Type header incorrectly e.g., by comparing it verbatim with "application/json" or clients that attempt to use some other than utf-8 encoding to decode the json text.

    May rfc-compliant software generate content type application/json with a charset parameter?

    no. No parameters are defined for application/json.

    Should rfc-compliant software accept such requests?

    yes, it should. The value of charset must be ignored.


    ECMA-404 (The JSON Data Interchange Format) defines json text in terms of Unicode code points i.e., json itself specifies no behavior regarding encoding details.

    ECMA-262 (ECMAScript Language Specification) also defines the json format on top of String (Unicode type).

提交回复
热议问题