Ajax Post提交事例及SpringMVC注解@RequestMapping取不到参数值解决办法

var  xmlHttp; //定义变量,用来建立xmlHttp对象
function  ajaxfunction(url,onreadystatechangMethod,param){ // 建立xmlHttp,ajax开始
     if (window.XMLHttpRequest){ //非IE浏览器及IE7(7.0及以上版本),用xmlHttp对象建立
         xmlHttp= new  XMLHttpRequest();
     } else  if (window.ActiveXObject){ //IE(6.0及如下版本)浏览器用activexobject对象建立,若是用户浏览器禁用了ActiveX,可能会失败.
         xmlHttp= new  ActiveXObject( "Microsoft.XMLHttp" );
     }
     
     if (xmlHttp){ //成功建立xmlHttp
         param=encodeURI(param);   //URL编辑,解决乱码问题
         param=encodeURI(param);
         xmlHttp.open( "post" ,url, false ); //与服务端创建链接(请求方式post或get,地址,true表示异步)
         xmlHttp.onreadystatechange = onreadystatechangMethod; //指定回调函数
         xmlHttp.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded;charset=UTF-8" ); //post提交设置项
         xmlHttp.send(param); //发送请求  
     }
}

 SpringMVC中的@RequestMapping修饰的方法在正常状况下虽然能够直接在参数列表中声明参数,但若是在Ajax的Post方式提交时是不会取到值的,因此要用最原始的方法获取参数,
 若是参数中有大量数据,最好用new String接收javascript

@RequestMapping (value = "/page/video/videoReply.do" )
     public  String videoReply(HttpServletRequest request,
             HttpServletResponse response) {
  String strId = request.getParameter( "strId" );
  String content = new  String(request.getParameter( "content" ));
    try  {
             content = java.net.URLDecoder.decode(content, "UTF-8" );
         } catch  (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
  return  null ;
}
相关文章
相关标签/搜索