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 ;
}
相關文章
相關標籤/搜索