使用ajax传一组数据给servlet
- 提问者网友:雨不眠的下
- 2021-05-01 08:42
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-05-01 09:09
这多麻烦啊,有session.setAttribue("","");
然后到servlet里用session.getAttribute(");取出来,这样多方便
- 1楼网友:像个废品
- 2021-05-01 09:25
用ajax实现的话首先创建Ajax对象httpReq,之后向服务器发送数据,再接受数据.大概流程如下,
如果有不明白的地方可以M我
httpReq = initAjax();
定义如下函数function initAjax() { var req = false; if(window.XMLHttpRequest) { req = new XMLHttpRequest(); if(req.overrideMimeType) { req.overrideMimeType('text/xml'); } } else if(window.ActiveXObject) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { req = new ActiveXObject("Msxml2.XMLHTTP"); } } if(!req) alert("ajax初始失败"); return req }
下面是向服务器发送数据的函数
function sendMsg() { if(httpReq.readyState==0||httpReq.readyState==4) { httpReq= initAjax(); var url = "/googleSugest/search"; httpReq.onreadystatechange = fromService; httpReq.open("POST",url,true); httpReq.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " ); httpReq.send("id=1&name=独饮月色") ; } }
function fromService() { }