A problem with JQuery Ajax is that there are special characters in the parameters, such as & ' #@, which can cause problems when executing Ajax, because the parameters that are passed are changed. Look at the example and you'll see:
Programme I:
$.ajax ({
URL: '/ashx/ajax.ashx ',
type: ' Post ',
data: ' Option=delete&name=11&adb, Success: function (data) {if (Data!= ' error ') {}}}); '
The Ajax performed above is the asynchronous deletion of a name-11&ABD data when the request to the Ajax.ashx page, we get the name parameter of 11 after the operation will find that the data is deleted with name 11 instead of deleting name as 11& ABC data This is because there are & special characters, the previous two parameters into three parameters option,name,abc then you need to pass the parameter in another way:
$.ajax ({
URL: '/ashx/ajax.ashx ',
type: ' Post ',
data: {
' option ': ' delete ',
' name ': ' 11&adb '
},
success:function (data) {
if (data!= ' error ') {}
}
});
Using the above JSON format to pass parameters can avoid the problem of parameter errors caused by special characters.
Programme two: Unified coding UTF-8.
1.JSP page:
<%@ page language= "java" pageencoding= "UTF-8"%>
2.ajax.js page: When passing parameters, the parameters of special characters may appear with Escape (encodeURIComponent ()) Two functions for transcoding, passed to the background!
var url = "/zx/servlet/addmemoservlet memo=" + Memocode + "&othermemo=" + Escape (encodeURIComponent (Othermemo)) + "&am" p;applno= "+ applNo.innerText.substr (0, 16); Alert ("Url=" +url);
Xmlhttp.open ("POST", url, true);
Xmlhttp.onreadystatechange = Domemo;
3. The server-side receives the delivered data for example: A servlet's Doget method: Request.setcharacterencoding ("gb2312"); Response.setcontenttype ("Text/xml;charset=utf-8"); Response.setheader ("Cache-control", "No-cache"); ...//The following address the URL passed in Ajax parameter value contains special characters, backend parsing error: To Utf-8 in the way to decode Java.net.URLDecoder urldecoder=new Java.net.URLDecoder (); String Othermemo = Urldecoder.decode (Request.getparameter ("Othermemo"), "Utf-8"); Logger.info ("Othermemo:" + Othermemo);
The above Ajax transfer parameters contain special characters of the fast solution is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.