为什么XMLHttpReq.responseText是空的?

yinyuj2me 2012-07-21 10:40:33
我用ajax做无闪烁刷新页面,结果用alert(XMLHttpReq.responseText)测试,结果为空,为什么XMLHttpReq.responseText是空的?
...全文
446 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinyuj2me 2012-07-21
  • 打赏
  • 举报
回复
不明白
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
if (XMLHttpReq.overrideMimeType) {//设置MiME类别
XMLHttpReq.overrideMimeType('text/xml');
}

else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {

createXMLHttpRequest();
var id=document.getElementById("id").value;
var url = "userInfo.do?action=updateResetMyselfs";
XMLHttpReq.open("GET", url, true);

XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLHttpReq.setRequestHeader("If-Modified-Since","0");
XMLHttpReq.send(id); // 发送请求
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息

alert(xml);
var xml=XMLHttpReq.responseText;

alert(xml);
document.getElementById("mobilePhone").innerHTML=xml
setInterval("sendRequest()",5000);

} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}


Action

public String updateResetMyselfs(HttpServletRequest request,
HttpServletResponse response, UserInfoForm userInfoForm, UserInfo uf) {

//response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control", "no-cache");
int id = uf.getId();
UserInfoService userInfoService = new UserInfoService();
UserInfo userInfo = userInfoService.getUserInfoById(id);
// ///////////////////////////////////////////////////////
String decPwd = ThreadDes.decryptMode(userInfo.getUserPwd());
userInfoForm.setId(userInfo.getId());
userInfoForm.setUserId(userInfo.getUserId());
userInfoForm.setUserPwd(decPwd);
userInfoForm.setNewUserPwd(decPwd);
userInfoForm.setNewUserPassword(decPwd);
userInfoForm.setUserName(userInfo.getUserName());
userInfoForm.setTelephone(userInfo.getTelephone());
userInfoForm.setMobilePhone(userInfo.getMobilePhone());
String MobilePhone=userInfo.getMobilePhone();
System.out.println(MobilePhone);
return null;
}
yinyuj2me 2012-07-21
  • 打赏
  • 举报
回复
不明白
var XMLHttpReq;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.XMLHttpRequest) { //Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
if (XMLHttpReq.overrideMimeType) {//设置MiME类别
XMLHttpReq.overrideMimeType('text/xml');
}

else if (window.ActiveXObject) { // IE浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
}
//发送请求函数
function sendRequest() {

createXMLHttpRequest();
var id=document.getElementById("id").value;
var url = "userInfo.do?action=updateResetMyselfs";
XMLHttpReq.open("GET", url, true);

XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLHttpReq.setRequestHeader("If-Modified-Since","0");
XMLHttpReq.send(id); // 发送请求
XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
}
// 处理返回信息函数
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判断对象状态
if (XMLHttpReq.status == 200) { // 信息已经成功返回,开始处理信息

alert(xml);
var xml=XMLHttpReq.responseText;

alert(xml);
document.getElementById("mobilePhone").innerHTML=xml
setInterval("sendRequest()",5000);

} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}


Action

public String updateResetMyselfs(HttpServletRequest request,
HttpServletResponse response, UserInfoForm userInfoForm, UserInfo uf) {

//response.setContentType("text/xml;charset=utf-8");
// response.setHeader("Cache-Control", "no-cache");
int id = uf.getId();
UserInfoService userInfoService = new UserInfoService();
UserInfo userInfo = userInfoService.getUserInfoById(id);
// ///////////////////////////////////////////////////////
String decPwd = ThreadDes.decryptMode(userInfo.getUserPwd());
userInfoForm.setId(userInfo.getId());
userInfoForm.setUserId(userInfo.getUserId());
userInfoForm.setUserPwd(decPwd);
userInfoForm.setNewUserPwd(decPwd);
userInfoForm.setNewUserPassword(decPwd);
userInfoForm.setUserName(userInfo.getUserName());
userInfoForm.setTelephone(userInfo.getTelephone());
userInfoForm.setMobilePhone(userInfo.getMobilePhone());
String MobilePhone=userInfo.getMobilePhone();
System.out.println(MobilePhone);
return null;
}
人生无悔 2012-07-21
  • 打赏
  • 举报
回复
那就说明后台返回的内容是空的,看下你后台如何返回的,断点到你写的方法

自已创建的XmlHttpRquest一般都写在ashx文件中的

使用ajax服务器控件吧,不用自已写XmlHttpRequest请求了,直接前台页面调用后台方法就好了

licip 2012-07-21
  • 打赏
  • 举报
回复
你单独运行你的发送ajax请求的url看看结果对不对。
孟子E章 2012-07-21
  • 打赏
  • 举报
回复
var url = "userInfo.do?action=updateResetMyselfs&id=" + id;


XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");这行删除,这是无用的


后台
request.getParameter("id")得到id查询数据

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧