A quick solution for ajax Transfer Parameters containing special characters.
JQuery AJAX encounters such a problem that the parameter contains special characters, such as & '# @. In this case, a problem occurs when AJAX is executed, because the parameter passed has changed. take a look at the example to understand:
Solution 1:
$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: 'option=delete&name=11&adb, success: function (data) { if (data != 'error ') { } } }); '
The ajax executed above is to asynchronously delete a data with name 11 & abd when the request is sent to ajax. on the ashx page, when we get the name parameter "11", we will find that the data whose name is 11 has been deleted, the data whose name is 11 & abc is not deleted. This is because there are & special characters, the previous two parameters are changed to three parameters: option, name, in this case, you need to use another method to pass the parameter:
$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: { 'option': 'delete', 'name': '11&adb' }, success: function(data) { if (data != 'error') {} }});
Passing parameters in json format can avoid parameter errors caused by special characters.
Solution 2: Unified coding UTF-8.
1. JSP page:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
2. Ajax. js page: When passing parameters, parameters with special characters may be transcoded using the escape (encodeURIComponent () function and passed to the background!
var url = "/ZX/servlet/AddMemoServlet memo=" + memoCode + "&otherMemo=" + escape(encodeURIComponent(otherMemo)) + "&applNo=" + applNo.innerText.substr(0, 16); //alert("url="+url); xmlHttp.open("POST", url, true); xmlHttp.onreadystatechange = doMemo; xmlHttp.send(null);
3. the data received by the server, for example, in the doGet method of a servlet: request. setCharacterEncoding ("gb2312"); response. setContentType ("text/xml; charset = UTF-8"); response. setHeader ("Cache-Control", "no-cache ");...... // The following solution solves the problem of backend parsing errors when url-passed parameter values contain special characters in Ajax: Decoding java.net using UTF-8. URLDecoder urlDecoder = new java.net. URLDecoder (); String otherMemo = urlDecoder. decode (request. getParameter ("otherMemo"), "UTF-8"); logger.info ("otherMemo:" + otherMemo );
The quick solution for the above ajax transmission parameters containing special characters is to share all the content with you in the editor. I hope you can give us a reference, and hope you can also support the help house.