text/plain 和application/json区别、Get参数带有特殊字符,需要encode、encodeURIComponent和encodeURI的区别
1. text/plain 和application/json区别
text/plain and application/json are two different MIME types used for representing data in HTTP requests and responses.
text/plain is a simple text format that represents plain text data without any specific format. It is generally used for text data that does not require any special formatting, such as plain text files or log files.
On the other hand, application/json is a structured format used to represent data in a structured way. It is used to exchange data between different applications that use different programming languages and technologies. JSON stands for “JavaScript Object Notation” and is a lightweight format that is easy to read and write for humans, and easy to parse and generate for machines.
The key difference between text/plain and application/json is that text/plain is used for simple text data, while application/json is used for structured data in a specific format. application/json is more widely used for exchanging data between different systems or applications, especially in web-based systems, while text/plain is more suitable for simple textual data.
例子:
原始的字符串:
“1COMPON=ISG,COMPID=SCSDS,ISSUER=ISGRREC ,MODULE=ISGGRT +???,ABEND=S0738,REASON=023E0004 1 22:23:14 03/30/23\n”
如果用text/plain返回: 每个反斜杠/之前都会自动给加上一个正斜杠
“1COMPON=ISG,COMPID=SCSDS,ISSUER=ISGRREC ,MODULE=ISGGRT +???,ABEND=S0738,REASON=023E0004 1 22:23:14 03/30/23\n”
As application/json: 不会自动加
"1COMPON=ISG,COMPID=SCSDS,ISSUER=ISGRREC ,MODULE=ISGGRT +???,ABEND=S0738,REASON=023E0004 1 22:23:14 03/30/23\n
2. Get参数带有特殊字符,需要encode
/**
* When passing the data from the front-end to the servlet using GET method, the data is usually
* encoded as a URL query parameter. In order to preserve the special characters like newline (\n),
* you can use the encodeURIComponent() function in JavaScript to encode the string before passing
* it as a query parameter.
*
* In the servlet, you can decode the query parameter using URLDecoder.decode() method to
* retrieve the original string.
*/
failData = URLDecoder.decode(failData, "UTF-8");
3. encodeURIComponent和encodeURI的区别
Both encodeURIComponent and encodeURI are functions in JavaScript used to encode a URI, but they have slightly different purposes.
encodeURIComponent is used to encode the individual query parameter values in a URL. It encodes all characters except for the following: A-Z a-z 0-9 - _ . ! ~ * ’ ( ). This function is typically used when you want to encode a specific part of a URL, such as a query parameter value, before sending it in an HTTP request. encodeURIComponent比encodeURI编码的字符范围更广,常用语encode query parameter.
encodeURI, on the other hand, is used to encode an entire URI. It encodes all characters except for the following: A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ’ ( ) #. This function is typically used when you want to encode an entire URL, including the scheme (http, https, etc.), hostname, path, and query parameters.
In summary, encodeURIComponent is used to encode individual query parameter values, while encodeURI is used to encode an entire URI, including the scheme, hostname, path, and query parameters.
更多推荐
所有评论(0)