Ext Ajax uses encodeURI garbled

Source: Internet
Author: User

Recently in the use of ExtJS for data submission, there will be garbled, nothing to say directly to the snippet code:

Add Point group var addpointgroup = function (groupName) {Ext.Ajax.request ({url:pg_servlet + ' _addfavoritegroup?name= ' + encodeURI (Magusencodeuri (GroupName)), method: ' POST ', success:function (response) {loadpointgroup (); Ext.Msg.alert (' Hint ', ' Add point Group succeeded! ');}, Failure:function (response) {Ext.Msg.alert (' hint ', ' Add point group failed! ' + response.responsetext) ;}});};

Magusencodeuri is a built-in transcoding device with the following code:

Magusencodeuri = function (text) {text = encodeURI (text); text = Text.replace (/\+%/g, "%20"); text = Text.replace (/\//g, "% 2F "); text = Text.replace (/\?/g,"%3f "); text = Text.replace (/\#/g,"%23 "); text = Text.replace (/\&/g,"%26 "); return text;}

when you do not use the outer layer of encodeURI, the Java background is always garbled.

Java Backend Code:

Public String Addfavoritegroup () throws Unsupportedencodingexception{parseparameter pp = Parseparameter.getparser (); String name = pp.parsestring ("name", request). toUpperCase (); Try{fgservice.addfavoritegroup (name); Jsonobject Jo = Getsuccessjson (favoritegroupservice.fun_addfavoritegroup); String result = Jo.tostring (); Response.setcharacterencoding ("Utf-8"); Response.getwriter (). Print (result); catch (Exception e) {e.printstacktrace (); return null;} return TEXT;}

where the pp.parsestring code is as follows:

public string parsestring (string fieldName, HttpServletRequest request) {string result = Request.getparameter (fieldName ); try{/** * Decodes the foreground encodeURI encoded letter */if (Result! = NULL &&! "). Equals (Result.trim ())) {result = Java.net.URLDecoder.decode (result, "UTF-8"); result = Converuricode (result); result = Result.trim ();}} catch (Unsupportedencodingexception e) {}return result;}

The Converuricode code is as follows:

private string Converuricode (string result) {//Add URL special symbol transcoding support if (Result.indexof ("%20")! =-1) {result = Result.replaceall ("%20", "+");} if (Result.indexof ("%2f")! =-1) {result = Result.replaceall ("%2f", "/");} if (Result.indexof ("%3f")! =-1) {result = Result.replaceall ("%3f", "?");} if (Result.indexof ("%23")! =-1) {result = Result.replaceall ("%23", "#");} if (Result.indexof ("%25")! =-1) {result = Result.replaceall ("%25", "%");} if (Result.indexof ("%26")! =-1) {result = Result.replaceall ("%26", "&");} return result;}

The function of the Converuricode code and in order to parse the Magusencodeuri code.

The use of the above set of methods can be guaranteed to pass over the parameters are not garbled (this premise is required, your HTML or JSP file to set the encoding format, or set the corresponding filter, to encode a strong format).

Here's a look at what this code should be aware of:

in the time of the special parameters to pass two times encodeURI, and the way to submit the best use of Post,get way may have unexpected garbled problem. In the front-end use what kind of transcoding in the background to use the same decoding method, corresponding to Magusencodeuri and Converuricode, and after Java.net.URLDecoder.deocde ( This place to pay special attention to, There are a lot of routines on the web that are written by Java.net.URIEncoder.encode, and I've been in the hole to decode. Other need to pay attention to the place is, in front of the background must use a unified encoding format, in the same project must be unified, otherwise there will be a variety of garbled questions .




Ext Ajax uses encodeURI garbled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.