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

本文详细介绍了如何使用Ajax技术实现与服务器的异步通信,并特别针对SpringMVC框架中的参数传递进行了深入探讨,包括如何正确处理POST请求中的中文乱码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/live365wang/archive/2011/03/26/1996266.html

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接收

@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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值