Request address how to effectively deal containing special characters #

  Today found that when the request parameter contains special characters, when certain parameter values ​​are passed to the back end has changed, for example, contains a # character in the request parameters during commissioning, the parameter value is not received back-end character # and its contents behind the characters in the Chrome browser, press the F12 can be seen in the Network address request in a request parameter with the # character, the # character and the following characters are truncated, so think of the need for such request parameter encoding process. Since this request is sent to the front when the choreographer created or computed method method called automatically Vue objects, so in order to make changes in the front-end is unlikely, and only in the back-end processing, but also for the back-end processing the front end of the call. That back end where the processing is better? There should be a place stitching request address it, we will tackle it from there, so that you can set up all of a certain type of parameter values. Because of this mosaic address to call for the front, so when using the encoding method is able to meet only the tip JS can call. Two front end contemplated herein encoding function: encodeURI and the encodeURIComponent, were added the following two functions to the encoding process parameter request, to see the effect.

(1) encodeURI function

Thus splicing a rear end address, for example: 'http' + '// IP:? Port / targetAddress param =' + encodeURI (value), wherein the value containing the value of the character #

The results show the browser: # character and the following characters are still cut off

(2) encodeURIComponent function

Thus splicing a rear end address, for example: 'http' + '// IP: port / targetAddress param =?' + EncodeURIComponent (value), wherein the value containing the value of the character #

The results show the browser: # character and the following characters are encoded, the whole value is the value of the string is encoded, parameters retained in the request.

From coding effect can be seen two functions: encodeURIComponent function characters may be encoded in the front end of the JS #, and function can not be encoded encodeURI character #, # character that still retained. Extending about: encodeURI () for encoding the entire URI, the URI as legitimate characters will not be converted encoded; encodeURIComponent method and coding for a single URIComponent (parameter request means) should be the most common, which can be Chinese parameters , escape special characters, but not the entire URL.

  While solving the problem of the # character as a request parameter # character and subsequent characters are truncated by encodeURIComponent function, but why would it be cut off? This is because the # character for URI, its role is to point to an anchor, for example:? Xx param = wb # gg, then this URL is actually targeted to xx param = wb this page ID for gg's? element position. So naturally the request parameters will not include the # character and the following characters, because of its special meaning determines its role.

      If the function does not want to automatically call request encodeURIComponent encode parameters, the request may be placed in the request body parameters, the parameters can be placed ajax data property, it will automatically call encodeURIComponent function data is encoded data.

 

------ 20,191,202 flash

Guess you like

Origin www.cnblogs.com/bien94/p/11972893.html