您好,欢迎访问一九零五行业门户网

ajax传送参数含有特殊字符解决方案

方案一:
$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: 'option=delete&name=11&adb, success: function (data) { if (data != 'error ') { } } }); '
上面执行的ajax就是异步删除一个name为 11&abd 的数据 当请求到ajax.ashx页面时,我们获取到的name参数为11 执行操作后会发现其实删除了name 为 11的数据,而没有删除 name 为 11&abc 的数据 这是由于有&特殊字符,把以前的俩个参数变成了三个参数 option,name,abc 这时就需要用另外一种方法传递参数:
$.ajax({ url: '/ashx/ajax.ashx', type: 'post', data: { 'option': 'delete', 'name': '11&adb' }, success: function(data) { if (data != 'error') {} }});
采用上面的json格式传递参数就可以避免特殊字符引起的参数错误问题.
方案二: 统一编码utf-8.
1.jsp页面:

2.ajax.js页面:传递参数时,可能出现特殊字符的参数用 escape(encodeuricomponent())两函数进行转码,传递到后台!
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.服务器端接收传递的数据 比如:一个servlet的doget方法中: request.setcharacterencoding(gb2312); response.setcontenttype(text/xml;charset=utf-8); response.setheader(cache-control, no-cache); ...... //以下解决ajax中url传递的参数值中包含特殊字符,后端解析出错的问题:以utf-8以方式解码 java.net.urldecoder urldecoder=new java.net.urldecoder(); string othermemo = urldecoder.decode(request.getparameter(othermemo),utf-8); logger.info(othermemo: + othermemo);
其它类似信息

推荐信息