URL encoding compatible with IE browser

Problem scenario: File upload can be operated normally in Google, but IE browser operation error occurs, and an error is reported in the service request. HTTP 400 Error - Bad request

 

Problem analysis: Comparing the two requests, it can be found that the request URLs of the two are inconsistent. JSP passes parameters to the backend through URL requests, but the incorrect format of the URL will cause the request to fail. This kind of URL problem often occurs in IE browser, and other browsers such as Firefox and Chrome will not have problems. Because Google will automatically encode spaces and some special characters, but IE does not have this step.

Solution: Manually encode the URL. Encode the url twice in the file upload method

encodeURI(encodeURI(url))

Knowledge involved in the above questions:

1. http request process:

  • The browser encodes the URL (and the content submitted by the post) and sends it to the server.
  •  The server will decode the content, process it, and return the result encoding to the browser.
  •  The browser displays the web page according to the specified encoding.

Character sets involved in string encoding and decoding, such as ISO8859-1, GBK, UTF-8, and UNICODE.

2. URL encoding

(1) URL encoding characteristics:

  • Because URLs often contain characters outside the ASCII set, the URL must be converted to valid ASCII format.
  • URL encoding uses "%" followed by two hexadecimal digits to replace non-ASCII characters.
  • URL cannot contain spaces. URL encoding usually uses + to replace spaces.

(2) URL encoding method

  • escape() cannot be used directly for URL encoding. Its real function is to return the Unicode encoded value of a character. escape() is not used for "+" encoding. It is mainly used for Chinese character encoding, and its use is no longer recommended.
  • encodeURI() is the function in Javascript that is actually used to encode URLs. The entire URL address is encoded, but the symbols with special meanings "; / ? : @ & = + $ , #" are not encoded. The corresponding decoding function is: decodeURI().
  • encodeURIComponent() can encode special characters such as "; / ? : @ & = + $ , #". The corresponding decoding function is decodeURIComponent(). If you want to pass a URL with an ampersand, use encodeURIComponent()

Code example:

代码:

var test1="http://www.haorooms.com/My name=hl&age=18/";
var enurl=encodeURI(test1);
console.log(enurl);
var enuc=encodeURIComponent(test1);
console.log(enuc);

//输出
http://www.haorooms.com/My%20name=hl&age=18/
http%3A%2F%2Fwww.haorooms.com%2FMy%20name%3Dhl%26age%3D18%2F

 

Guess you like

Origin blog.csdn.net/layliangbo/article/details/111497910