Ajax passed parameters contain special characters solution

Ajax passed parameters contain special characters solution

quote

       There is such a problem in JQuery AJAX. The parameters contain special characters, such as &'#@, etc. 
       This is a problem when executing AJAX, because the passed parameters have changed. 
       See an example to understand: 



Java code   Favorite code
  1. Option One:  
  2.               $.ajax({  
  3.                     url: '/ashx/ajax.ashx',  
  4.                     type: 'post',  
  5.                     data: 'option=delete&name=11&adb,  
  6.                     success: function (data) {  
  7.                         if (data != 'error') {  
  8.                             
  9.                             }  
  10.                         }  
  11.                });  
  12.        The ajax executed above is to asynchronously delete a data whose name is  11 &abd  
  13.        When requesting to the ajax.ashx page, the name parameter we get is 11  
  14.        After performing the operation, you will find that the data whose name is 11 is actually deleted  , but the data whose name is 11 &abc is   not deleted  .
  15.        This is due to the & special character, which turns the previous two parameters into three parameters option, name, abc  
  16.        Then you need to use another method to pass parameters:  
  17.               $.ajax({  
  18.                     url: '/ashx/ajax.ashx',  
  19.                     type: 'post',  
  20.                     data:{ 'option':'delete','name':'11&adb'},  
  21.                     success: function (data) {  
  22.                         if (data != 'error') {  
  23.                             
  24.                             }  
  25.                         }  
  26.                });  
  27.        Passing parameters in the above json format can avoid parameter errors caused by special characters.  

Java code   Favorite code
  1. Option II:  
  2.        统一编码UTF-8.  
  3.        1.JSP页面:<%@ page language="java" pageEncoding="UTF-8"%>  
  4.        2.Ajax.js页面:传递参数时,可能出现特殊字符的参数用                                             
  5.          escape(encodeURIComponent())两函数进行转码,传递到后台!  
  6.          var url = "/ZX/servlet/AddMemoServlet?memo=" + memoCode + "&otherMemo=" + escape (encodeURIComponent(otherMemo)) + "&applNo=" + applNo.innerText.substr(0,16);    
  7.         //alert("url="+url);  
  8.         xmlHttp.open("POST", url, true);  
  9.         xmlHttp.onreadystatechange = doMemo;  
  10.         xmlHttp.send(null);  
  11.        3.服务器端接收传递的数据 比如:一个servlet的doGet方法中:  
  12.          request.setCharacterEncoding("gb2312");  
  13.          response.setContentType("text/xml;charset=utf-8");  
  14.          response.setHeader("Cache-Control""no-cache");  
  15.   ......   
  16.        //以下解决Ajax中url传递的参数值中包含特殊字符,后端解析出错的问题:以utf-8以方式解码  
  17.          java.net.URLDecoder urlDecoder=new java.net.URLDecoder();  
  18.          String otherMemo = urlDecoder.decode(request.getParameter("otherMemo"),"utf-8");  
  19.          logger.info("otherMemo:" + otherMemo);  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524474&siteId=291194637