怎样在struts2里实现ajax,下面代码怎么实现不了

varxmlHttpReq;functioncreateXMLHttpRequest(){if(window.XMLHttpRequest){xmlHttpReq=new... var xmlHttpReq;
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}//Send Request
function sendRequest(url) {
createXMLHttpRequest();
xmlHttpReq.open("GET", url, true);
xmlHttpReq.onreadystatechange = processResponse;
xmlHttpReq.send(null);
}
function processResponse() {
if (xmlHttpReq.readyState == 4) {
if (xmlHttpReq.status == 200) {
var res = xmlHttpReq.responseText;
window.alert(res);
} else {
window.alert("An Error occured");
}
}
}
function validate(op) {
if (op == "") {
document.getElementById("sr").innerHTML = "<font color='red'>请输入用户名</font>";
} else {
sendRequest("weiyi!wy.action?username=" + op);
}
} 
XML里是
<package name="TestAjax" extends="json-default">
<action name="weiyi" class="myaction.Login" method="wy">
<result type="json" />
</action>
</package>
展开
 我来答
Iridescent_M
2014-04-08
知道答主
回答量:5
采纳率:0%
帮助的人:4.9万
展开全部
//jsp页面
function checkUsr(usrId){
if(usrId == "" || usrId.length <5){
return ;
}
$.ajax({
type :"post",
url :"<%=root%>/loginUser_returnUsr.action",
data :"usrId=" + usrId,
dataType :"json",
async:false,
success :function (data){
for(var x=0;x<data.length;x++){
if(data[x].message == 1){
document.getElementById("usrErrorMessage").innerHTML = "该用户名已被占用";
}else{
document.getElementById("usrErrorMessage").innerHTML = "";
}
}
}
});
}
//action中代码

public String returnUsr() {
HttpServletRequest request = ServletActionContext.getRequest();
String usrId = request.getParameter("usrId");
LoginUser loginUser = loginUserService.selectByUid(usrId);
String info = "";
info = "[";
if(loginUser == null){
info += "{";
info += "message:'0'";
info += "}";
}else{
info += "{";
info += "message:'1'";
info += "}";
}
info += "]";
writeString(info);
return null;
}

public void writeString(String info) {
HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter printWriter;
try {
printWriter = new java.io.PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
printWriter.write(info);
printWriter.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

XML里就不需要配置了,把我的service和路径改成你的,返回值自己定义一下就可以了。
追问
你那是用的jquery,能不能不用jquery,我还没学
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式